{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Installing packages\n",
    "\n",
    "When installing the version of Pandas needed for this exercise, please ignore the **incompatibility ERROR** that may appear for the packages visions and pandas-profiling. These are not needed.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pandas in /opt/conda/lib/python3.10/site-packages (1.5.3)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/conda/lib/python3.10/site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.10/site-packages (from pandas) (2023.3)\n",
      "Requirement already satisfied: numpy>=1.21.0 in /opt/conda/lib/python3.10/site-packages (from pandas) (1.23.5)\n",
      "Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Requirement already satisfied: plotly in /opt/conda/lib/python3.10/site-packages (5.14.1)\n",
      "Requirement already satisfied: tenacity>=6.2.0 in /opt/conda/lib/python3.10/site-packages (from plotly) (8.2.2)\n",
      "Requirement already satisfied: packaging in /opt/conda/lib/python3.10/site-packages (from plotly) (23.1)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Requirement already satisfied: tensorboardX in /opt/conda/lib/python3.10/site-packages (2.6)\n",
      "Requirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (from tensorboardX) (1.23.5)\n",
      "Requirement already satisfied: packaging in /opt/conda/lib/python3.10/site-packages (from tensorboardX) (23.1)\n",
      "Requirement already satisfied: protobuf<4,>=3.8.0 in /opt/conda/lib/python3.10/site-packages (from tensorboardX) (3.20.3)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Requirement already satisfied: bs4 in /home/jupyter/.local/lib/python3.10/site-packages (0.0.1)\n",
      "Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.10/site-packages (from bs4) (4.12.2)\n",
      "Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.10/site-packages (from beautifulsoup4->bs4) (2.3.2.post1)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Requirement already satisfied: auquan_toolbox in /home/jupyter/.local/lib/python3.10/site-packages (2.1.92)\n",
      "Requirement already satisfied: requests in /opt/conda/lib/python3.10/site-packages (from auquan_toolbox) (2.28.2)\n",
      "Requirement already satisfied: plotly in /opt/conda/lib/python3.10/site-packages (from auquan_toolbox) (5.14.1)\n",
      "Requirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (from auquan_toolbox) (1.23.5)\n",
      "Requirement already satisfied: pandas in /opt/conda/lib/python3.10/site-packages (from auquan_toolbox) (1.5.3)\n",
      "Requirement already satisfied: bs4 in /home/jupyter/.local/lib/python3.10/site-packages (from auquan_toolbox) (0.0.1)\n",
      "Requirement already satisfied: scipy in /opt/conda/lib/python3.10/site-packages (from auquan_toolbox) (1.9.3)\n",
      "Requirement already satisfied: sklearn in /home/jupyter/.local/lib/python3.10/site-packages (from auquan_toolbox) (0.0.post5)\n",
      "Requirement already satisfied: lxml in /home/jupyter/.local/lib/python3.10/site-packages (from auquan_toolbox) (4.9.2)\n",
      "Requirement already satisfied: tensorboard in /home/jupyter/.local/lib/python3.10/site-packages (from auquan_toolbox) (2.13.0)\n",
      "Requirement already satisfied: tensorboardX in /opt/conda/lib/python3.10/site-packages (from auquan_toolbox) (2.6)\n",
      "Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.10/site-packages (from bs4->auquan_toolbox) (4.12.2)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/conda/lib/python3.10/site-packages (from pandas->auquan_toolbox) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.10/site-packages (from pandas->auquan_toolbox) (2023.3)\n",
      "Requirement already satisfied: tenacity>=6.2.0 in /opt/conda/lib/python3.10/site-packages (from plotly->auquan_toolbox) (8.2.2)\n",
      "Requirement already satisfied: packaging in /opt/conda/lib/python3.10/site-packages (from plotly->auquan_toolbox) (23.1)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests->auquan_toolbox) (3.1.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.10/site-packages (from requests->auquan_toolbox) (3.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/lib/python3.10/site-packages (from requests->auquan_toolbox) (1.26.15)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.10/site-packages (from requests->auquan_toolbox) (2022.12.7)\n",
      "Requirement already satisfied: absl-py>=0.4 in /opt/conda/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (1.4.0)\n",
      "Requirement already satisfied: grpcio>=1.48.2 in /opt/conda/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (1.51.3)\n",
      "Requirement already satisfied: google-auth<3,>=1.6.3 in /opt/conda/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (2.17.3)\n",
      "Requirement already satisfied: google-auth-oauthlib<1.1,>=0.5 in /opt/conda/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (1.0.0)\n",
      "Requirement already satisfied: markdown>=2.6.8 in /home/jupyter/.local/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (3.4.3)\n",
      "Requirement already satisfied: protobuf>=3.19.6 in /opt/conda/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (3.20.3)\n",
      "Requirement already satisfied: setuptools>=41.0.0 in /opt/conda/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (67.7.2)\n",
      "Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /home/jupyter/.local/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (0.7.0)\n",
      "Requirement already satisfied: werkzeug>=1.0.1 in /home/jupyter/.local/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (2.3.6)\n",
      "Requirement already satisfied: wheel>=0.26 in /opt/conda/lib/python3.10/site-packages (from tensorboard->auquan_toolbox) (0.40.0)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /opt/conda/lib/python3.10/site-packages (from google-auth<3,>=1.6.3->tensorboard->auquan_toolbox) (5.3.0)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/lib/python3.10/site-packages (from google-auth<3,>=1.6.3->tensorboard->auquan_toolbox) (0.3.0)\n",
      "Requirement already satisfied: six>=1.9.0 in /opt/conda/lib/python3.10/site-packages (from google-auth<3,>=1.6.3->tensorboard->auquan_toolbox) (1.16.0)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in /opt/conda/lib/python3.10/site-packages (from google-auth<3,>=1.6.3->tensorboard->auquan_toolbox) (4.9)\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/conda/lib/python3.10/site-packages (from google-auth-oauthlib<1.1,>=0.5->tensorboard->auquan_toolbox) (1.3.1)\n",
      "Requirement already satisfied: MarkupSafe>=2.1.1 in /opt/conda/lib/python3.10/site-packages (from werkzeug>=1.0.1->tensorboard->auquan_toolbox) (2.1.2)\n",
      "Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.10/site-packages (from beautifulsoup4->bs4->auquan_toolbox) (2.3.2.post1)\n",
      "Requirement already satisfied: pyasn1<0.6.0,>=0.4.6 in /opt/conda/lib/python3.10/site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard->auquan_toolbox) (0.5.0)\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in /opt/conda/lib/python3.10/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<1.1,>=0.5->tensorboard->auquan_toolbox) (3.2.2)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Requirement already satisfied: yfinance in /opt/conda/lib/python3.10/site-packages (0.2.20)\n",
      "Requirement already satisfied: pandas>=1.3.0 in /opt/conda/lib/python3.10/site-packages (from yfinance) (1.5.3)\n",
      "Requirement already satisfied: numpy>=1.16.5 in /opt/conda/lib/python3.10/site-packages (from yfinance) (1.23.5)\n",
      "Requirement already satisfied: requests>=2.26 in /opt/conda/lib/python3.10/site-packages (from yfinance) (2.28.2)\n",
      "Requirement already satisfied: multitasking>=0.0.7 in /opt/conda/lib/python3.10/site-packages (from yfinance) (0.0.11)\n",
      "Requirement already satisfied: lxml>=4.9.1 in /home/jupyter/.local/lib/python3.10/site-packages (from yfinance) (4.9.2)\n",
      "Requirement already satisfied: appdirs>=1.4.4 in /opt/conda/lib/python3.10/site-packages (from yfinance) (1.4.4)\n",
      "Requirement already satisfied: pytz>=2022.5 in /opt/conda/lib/python3.10/site-packages (from yfinance) (2023.3)\n",
      "Requirement already satisfied: frozendict>=2.3.4 in /opt/conda/lib/python3.10/site-packages (from yfinance) (2.3.8)\n",
      "Requirement already satisfied: cryptography>=3.3.2 in /opt/conda/lib/python3.10/site-packages (from yfinance) (40.0.2)\n",
      "Requirement already satisfied: beautifulsoup4>=4.11.1 in /opt/conda/lib/python3.10/site-packages (from yfinance) (4.12.2)\n",
      "Requirement already satisfied: html5lib>=1.1 in /opt/conda/lib/python3.10/site-packages (from yfinance) (1.1)\n",
      "Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.10/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.3.2.post1)\n",
      "Requirement already satisfied: cffi>=1.12 in /opt/conda/lib/python3.10/site-packages (from cryptography>=3.3.2->yfinance) (1.15.1)\n",
      "Requirement already satisfied: six>=1.9 in /opt/conda/lib/python3.10/site-packages (from html5lib>=1.1->yfinance) (1.16.0)\n",
      "Requirement already satisfied: webencodings in /opt/conda/lib/python3.10/site-packages (from html5lib>=1.1->yfinance) (0.5.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/conda/lib/python3.10/site-packages (from pandas>=1.3.0->yfinance) (2.8.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests>=2.26->yfinance) (3.1.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.10/site-packages (from requests>=2.26->yfinance) (3.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/lib/python3.10/site-packages (from requests>=2.26->yfinance) (1.26.15)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.10/site-packages (from requests>=2.26->yfinance) (2022.12.7)\n",
      "Requirement already satisfied: pycparser in /opt/conda/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=3.3.2->yfinance) (2.21)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install pandas --user\n",
    "%pip install plotly\n",
    "%pip install tensorboardX --user\n",
    "%pip install bs4 --user\n",
    "%pip install -U auquan_toolbox --user\n",
    "%pip install yfinance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Update package\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "outputs": [],
   "execution_count": 2,
   "source": [
    "!cp ~/training-data-analyst/courses/ai-for-finance/solution/yahoo_data_source.py ~/.local/lib/python3.10/site-packages/backtester/dataSource/\n",
    "!cp ~/training-data-analyst/courses/ai-for-finance/solution/data_source_utils.py ~/.local/lib/python3.10/site-packages/backtester/dataSource/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "WsNIiolz3r9C"
   },
   "source": [
    "### This notebook shows how [Auquan Toolbox](https://github.com/Auquan/Tutorials/blob/master/momentum_backtest_losing_money.ipynb) can be used to trade on momentum and mean reversion\n",
    "\n",
    "Documentation on how to use the toolbox can be found [here](https://github.com/Auquan/auquan-toolbox-python#3-backtesting). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note**: When running the cell below, if you get a module not found error for the backtester library, please restart the Kernel for the package update to be detected."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 381
    },
    "colab_type": "code",
    "id": "IjSoh14b3r9G",
    "outputId": "e8afb1bc-8564-47ff-a067-f5970d7fa3d3"
   },
   "outputs": [],
   "source": [
    "from backtester.trading_system_parameters import TradingSystemParameters\n",
    "from backtester.features.feature import Feature\n",
    "from backtester.dataSource.yahoo_data_source import YahooStockDataSource\n",
    "from backtester.timeRule.custom_time_rule import CustomTimeRule\n",
    "from backtester.executionSystem.simple_execution_system import SimpleExecutionSystem\n",
    "from backtester.orderPlacer.backtesting_order_placer import BacktestingOrderPlacer\n",
    "from backtester.trading_system import TradingSystem\n",
    "from backtester.constants import *\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import datetime\n",
    "from datetime import timedelta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "yaqazil43r9P"
   },
   "source": [
    "### This is the static part of an experiment\n",
    "\n",
    "It is similar to writing a data loader and trainer for a deep learning project. Once written for a particular experiment we hardly need to change it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "YBS5-alV3r9S"
   },
   "outputs": [],
   "source": [
    "class MyTradingParams(TradingSystemParameters):\n",
    "\n",
    "    def __init__(self, tradingFunctions):\n",
    "        self.__tradingFunctions = tradingFunctions\n",
    "        super(MyTradingParams, self).__init__()\n",
    "        self.__dataSetId = 'equity_data'\n",
    "        self.__instrumentIds = self.__tradingFunctions.getSymbolsToTrade()\n",
    "        self.__startDate = '2015/01/02'\n",
    "        self.__endDate = '2017/08/31'\n",
    "\n",
    "    def getDataParser(self):\n",
    "        '''\n",
    "        Returns an instance of class DataParser. Source of data for instruments\n",
    "        '''\n",
    "        instrumentIds = self.__tradingFunctions.getSymbolsToTrade()\n",
    "        return YahooStockDataSource(\n",
    "            cachedFolderName = 'historicalData/',\n",
    "            dataSetId = self.__dataSetId,\n",
    "            instrumentIds = instrumentIds,\n",
    "            startDateStr = self.__startDate,\n",
    "            endDateStr = self.__endDate,\n",
    "        )\n",
    "        \n",
    "    def getTimeRuleForUpdates(self):\n",
    "        return CustomTimeRule(\n",
    "            startDate = self.__startDate, \n",
    "            endDate = self.__endDate, \n",
    "            frequency = 'D', \n",
    "            sample = '30'\n",
    "        )\n",
    "\n",
    "    def getFrequencyOfFeatureUpdates(self):\n",
    "        return timedelta(days = 1)\n",
    "\n",
    "    def getStartingCapital(self):\n",
    "        if len(self.__tradingFunctions.getSymbolsToTrade()) > 0:\n",
    "            return 1000*len(self.__tradingFunctions.getSymbolsToTrade())\n",
    "        else:\n",
    "            return 30000\n",
    "\n",
    "    def getCustomFeatures(self):\n",
    "        '''\n",
    "        This is a way to use any custom features you might have made.\n",
    "        Returns a dictionary where:\n",
    "        \n",
    "        key: featureId to access this feature (Make sure this doesnt conflict with any of the pre defined feature Ids)\n",
    "        value: Your custom Class which computes this feature. The class should be an instance of Feature\n",
    "        \n",
    "        Eg. if your custom class is MyCustomFeature, and you want to access this via featureId='my_custom_feature',\n",
    "        you will import that class, and return this function as {'my_custom_feature': MyCustomFeature}\n",
    "        '''\n",
    "        return {\n",
    "            'my_custom_feature': MyCustomFeature,\n",
    "            'prediction': TrainingPredictionFeature,\n",
    "            'zero_fees': FeesCalculator,\n",
    "            'benchmark_PnL': BuyHoldPnL,\n",
    "            'score': ScoreFeature\n",
    "        }\n",
    "\n",
    "    def getInstrumentFeatureConfigDicts(self):\n",
    "        '''\n",
    "        Returns an array of instrument feature config dictionaries instrument feature config Dictionary has the \n",
    "        following keys:\n",
    "        \n",
    "        featureId: a string representing the type of feature you want to use\n",
    "        featureKey: a string representing the key you will use to access the value of this feature\n",
    "        params: A dictionary with which contains other optional params if needed by the feature\n",
    "        '''\n",
    "\n",
    "        predictionDict = {\n",
    "            'featureKey': 'prediction',\n",
    "            'featureId': 'prediction',\n",
    "            'params': {}\n",
    "        }\n",
    "        feesConfigDict = {\n",
    "            'featureKey': 'fees',\n",
    "            'featureId': 'zero_fees',\n",
    "            'params': {}\n",
    "        }\n",
    "        profitlossConfigDict = {\n",
    "            'featureKey': 'pnl',\n",
    "            'featureId': 'pnl',\n",
    "            'params': {\n",
    "                'price': self.getPriceFeatureKey(),\n",
    "                'fees': 'fees'\n",
    "            }\n",
    "        }\n",
    "        capitalConfigDict = {\n",
    "            'featureKey': 'capital',\n",
    "            'featureId': 'capital',\n",
    "            'params': {\n",
    "                'price': 'adjClose',\n",
    "                'fees': 'fees',\n",
    "                'capitalReqPercent': 0.95\n",
    "            }\n",
    "        }\n",
    "        benchmarkDict = {\n",
    "            'featureKey': 'benchmark',\n",
    "            'featureId': 'benchmark_PnL',\n",
    "            'params': {'pnlKey': 'pnl'}\n",
    "        }\n",
    "        scoreDict = {\n",
    "            'featureKey': 'score',\n",
    "            'featureId': 'score',\n",
    "            'params': {\n",
    "                'featureName1': 'pnl',\n",
    "                'featureName2':'benchmark'\n",
    "            }\n",
    "        }\n",
    "\n",
    "        stockFeatureConfigs = self.__tradingFunctions.getInstrumentFeatureConfigDicts()\n",
    "\n",
    "        return {\n",
    "            INSTRUMENT_TYPE_STOCK: stockFeatureConfigs + [\n",
    "                predictionDict,\n",
    "                feesConfigDict,\n",
    "                profitlossConfigDict,\n",
    "                capitalConfigDict,\n",
    "                benchmarkDict, \n",
    "                scoreDict\n",
    "            ]\n",
    "        }\n",
    "\n",
    "    def getMarketFeatureConfigDicts(self):\n",
    "        '''\n",
    "        Returns an array of market feature config dictionaries having the following keys:\n",
    "        \n",
    "        featureId: a string representing the type of feature you want to use\n",
    "        featureKey: a string representing the key you will use to access the value of this feature\n",
    "        params: A dictionary with which contains other optional params if needed by the feature\n",
    "        '''\n",
    "        scoreDict = {\n",
    "            'featureKey': 'score',\n",
    "            'featureId': 'score_ll',\n",
    "            'params': {\n",
    "                'featureName': self.getPriceFeatureKey(),\n",
    "                'instrument_score_feature': 'score'\n",
    "            }\n",
    "        }\n",
    "        \n",
    "        return [scoreDict]\n",
    "\n",
    "    def getPrediction(self, time, updateNum, instrumentManager):\n",
    "        predictions = pd.Series(index = self.__instrumentIds)\n",
    "        predictions = self.__tradingFunctions.getPrediction(time, updateNum, instrumentManager, predictions)\n",
    "        \n",
    "        return predictions\n",
    "\n",
    "    def getExecutionSystem(self):\n",
    "        '''\n",
    "        Returns the type of execution system we want to use. Its an implementation of the class ExecutionSystem\n",
    "        It converts prediction to intended positions for different instruments.\n",
    "        '''\n",
    "        \n",
    "        return SimpleExecutionSystem(\n",
    "            enter_threshold = 0.7,\n",
    "            exit_threshold = 0.55,\n",
    "            longLimit = 1,\n",
    "            shortLimit = 1,\n",
    "            capitalUsageLimit = 0.10*self.getStartingCapital(),\n",
    "            enterlotSize = 1, \n",
    "            exitlotSize = 1,\n",
    "            limitType = 'L', \n",
    "            price = 'adjClose'\n",
    "        )\n",
    "\n",
    "    def getOrderPlacer(self):\n",
    "        '''\n",
    "        Returns the type of order placer we want to use. It's an implementation of the class OrderPlacer.\n",
    "        It helps place an order, and also read confirmations of orders being placed.\n",
    "        For Backtesting, you can just use the BacktestingOrderPlacer, which places the order which you want, and \n",
    "        automatically confirms it too.\n",
    "        '''\n",
    "        \n",
    "        return BacktestingOrderPlacer()\n",
    "\n",
    "    def getLookbackSize(self):\n",
    "        '''\n",
    "        Returns the amount of lookback data you want for your calculations. The historical market features and instrument features are only\n",
    "        stored upto this amount.\n",
    "        This number is the number of times we have updated our features.\n",
    "        '''\n",
    "        \n",
    "        return 120\n",
    "\n",
    "    def getPriceFeatureKey(self):\n",
    "        '''\n",
    "        The name of column containing the instrument price\n",
    "        '''\n",
    "        \n",
    "        return 'adjClose'\n",
    "\n",
    "    def getInstrumentsIds(self):\n",
    "        '''\n",
    "        Get all instrument ids\n",
    "        '''\n",
    "        \n",
    "        return self.__instrumentIds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Hkb02Y1i3r9b"
   },
   "source": [
    "### Let's define some of our own features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "8EmCAvit3r9e"
   },
   "outputs": [],
   "source": [
    "class TrainingPredictionFeature(Feature):\n",
    "    \n",
    "    @classmethod\n",
    "    def computeForInstrument(cls, updateNum, time, featureParams, featureKey, instrumentManager):\n",
    "        tf = MyTradingFunctions()\n",
    "        t = MyTradingParams(tf)\n",
    "        \n",
    "        return t.getPrediction(time, updateNum, instrumentManager)\n",
    "\n",
    "class FeesCalculator(Feature):\n",
    "    @classmethod\n",
    "    def computeForInstrument(cls, updateNum, time, featureParams, featureKey, instrumentManager):\n",
    "        \n",
    "        return pd.Series(0, index = instrumentManager.getAllInstrumentsByInstrumentId())\n",
    "\n",
    "class BuyHoldPnL(Feature):\n",
    "    \n",
    "    @classmethod\n",
    "    def computeForInstrument(cls, updateNum, time, featureParams, featureKey, instrumentManager):\n",
    "        instrumentLookbackData = instrumentManager.getLookbackInstrumentFeatures()\n",
    "\n",
    "        priceData = instrumentLookbackData.getFeatureDf('adjClose')\n",
    "         \n",
    "        if len(priceData) < 2:\n",
    "            return pd.Series(0, index = instrumentManager.getAllInstrumentsByInstrumentId())\n",
    "        else:\n",
    "            bhpnl = instrumentLookbackData.getFeatureDf(featureKey).iloc[-1]\n",
    "            bhpnl += priceData.iloc[-1] - priceData.iloc[-2]\n",
    "\n",
    "        return bhpnl\n",
    "\n",
    "class ScoreFeature(Feature):\n",
    "    \n",
    "    @classmethod\n",
    "    def computeForInstrument(cls, updateNum, time, featureParams, featureKey, instrumentManager):\n",
    "        instrumentLookbackData = instrumentManager.getLookbackInstrumentFeatures()\n",
    "        if len(instrumentLookbackData.getFeatureDf(featureParams['featureName1'])) > 0:\n",
    "            feature1 = instrumentLookbackData.getFeatureDf(featureParams['featureName1']).iloc[-1]\n",
    "            feature2 = instrumentLookbackData.getFeatureDf(featureParams['featureName2']).iloc[-1]\n",
    "            \n",
    "            for instrumentId in feature1.index:\n",
    "                pnls = instrumentLookbackData.getFeatureDf('pnl')[instrumentId]\n",
    "                positions = instrumentLookbackData.getFeatureDf('position')[instrumentId]\n",
    "                \n",
    "                print(instrumentId)\n",
    "                print('pnl: %.2f'%pnls[-1])\n",
    "                if len(positions) > 2 and np.abs(positions[-1] - positions[-2]) > 0:\n",
    "                    print('Position changed to: %.2f'%positions[-1])\n",
    "            \n",
    "            toRtn = (feature1 - feature2) / feature2.abs()\n",
    "            toRtn[toRtn.isnull()] = 0\n",
    "            toRtn[toRtn == np.Inf] = 0\n",
    "        else:\n",
    "            toRtn=0\n",
    "\n",
    "        return toRtn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "_YeUN2jV3r9m"
   },
   "source": [
    "### This is the part where the magic takes place, all the logic for prediction and carrying out trades goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "TmmI1xdA3r9o"
   },
   "outputs": [],
   "source": [
    "class MyTradingFunctions():\n",
    "\n",
    "    def __init__(self):\n",
    "        self.count = 0\n",
    "        self.params = {}\n",
    "\n",
    "    def getSymbolsToTrade(self):\n",
    "        '''\n",
    "        Specify the stock names that you want to trade.\n",
    "        '''\n",
    "        \n",
    "        return ['AAPL']\n",
    "\n",
    "    def getInstrumentFeatureConfigDicts(self):\n",
    "        '''\n",
    "        Specify all Features you want to use by creating config dictionaries.\n",
    "        Create one dictionary per feature and return them in an array.\n",
    "\n",
    "        Feature config Dictionary have the following keys:\n",
    "\n",
    "        featureId: a str for the type of feature you want to use\n",
    "        featureKey: {optional} a str for the key you will use to call this feature\n",
    "                    If not present, will just use featureId\n",
    "        params: {optional} A dictionary with which contains other optional params if needed by the feature\n",
    "\n",
    "        msDict = {\n",
    "            'featureKey': 'ms_5',\n",
    "            'featureId': 'moving_sum',\n",
    "            'params': {\n",
    "                'period': 5,\n",
    "                'featureName': 'basis'\n",
    "            }\n",
    "        }\n",
    "\n",
    "        return [msDict]\n",
    "\n",
    "        You can now use this feature by in getPRediction() calling it's featureKey, 'ms_5'\n",
    "        '''\n",
    "\n",
    "        ma1Dict = {\n",
    "            'featureKey': 'ma_90',\n",
    "            'featureId': 'moving_average',\n",
    "            'params': {\n",
    "                'period': 90,\n",
    "                'featureName': 'adjClose'\n",
    "            }\n",
    "        }\n",
    "        mom30Dict = {\n",
    "            'featureKey': 'mom_30',\n",
    "            'featureId': 'momentum',\n",
    "            'params': {\n",
    "                'period': 30,\n",
    "                'featureName': 'adjClose'\n",
    "            }\n",
    "        }\n",
    "        mom10Dict = {\n",
    "            'featureKey': 'mom_10',\n",
    "            'featureId': 'momentum',\n",
    "            'params': {\n",
    "                'period': 10,\n",
    "                'featureName': 'adjClose'\n",
    "            }\n",
    "        }\n",
    "        \n",
    "        return [ma1Dict, mom10Dict, mom30Dict]\n",
    "\n",
    "    def getPrediction(self, time, updateNum, instrumentManager, predictions):\n",
    "        '''\n",
    "        Combine all the features to create the desired predictions for each stock.\n",
    "        'predictions' is Pandas Series with stock as index and predictions as values\n",
    "        We first call the holder for all the instrument features for all stocks as\n",
    "            lookbackInstrumentFeatures = instrumentManager.getLookbackInstrumentFeatures()\n",
    "        Then call the dataframe for a feature using its feature_key as\n",
    "            ms5Data = lookbackInstrumentFeatures.getFeatureDf('ms_5')\n",
    "        This returns a dataFrame for that feature for ALL stocks for all times upto lookback time\n",
    "        Now you can call just the last data point for ALL stocks as\n",
    "            ms5 = ms5Data.iloc[-1]\n",
    "        You can call last datapoint for one stock 'ABC' as\n",
    "            value_for_abs = ms5['ABC']\n",
    "\n",
    "        Output of the prediction function is used by the toolbox to make further trading decisions and evaluate your score.\n",
    "        '''\n",
    "\n",
    "        # self.updateCount() - uncomment if you want a counter\n",
    "\n",
    "        # holder for all the instrument features for all instruments\n",
    "        lookbackInstrumentFeatures = instrumentManager.getLookbackInstrumentFeatures()\n",
    "\n",
    "        #############################################################################################\n",
    "        ### TODO : FILL THIS FUNCTION TO RETURN A BUY (1) or SELL (0) prediction for each stock  ###\n",
    "        ### USE TEMPLATE BELOW AS EXAMPLE\n",
    "        ###\n",
    "        ### HINT: Use the Hurst Exponent \n",
    "        ### http://analytics-magazine.org/the-hurst-exponent-predictability-of-time-series/\n",
    "        #############################################################################################\n",
    "        \n",
    "        # Here's an example implementation of the hurst exponent\n",
    "        def hurst_f(input_ts, lags_to_test=20):  \n",
    "            # interpretation of return value\n",
    "            # hurst < 0.5 - input_ts is mean reverting\n",
    "            # hurst = 0.5 - input_ts is effectively random/geometric brownian motion\n",
    "            # hurst > 0.5 - input_ts is trending\n",
    "            tau = []\n",
    "            lagvec = []  \n",
    "            #  Step through the different lags  \n",
    "            for lag in range(2, lags_to_test):  \n",
    "                #  produce price difference with lag  \n",
    "                pp = np.subtract(input_ts[lag:], input_ts[:-lag])  \n",
    "                #  Write the different lags into a vector  \n",
    "                lagvec.append(lag)  \n",
    "                #  Calculate the variance of the differnce vector  \n",
    "                tau.append(np.sqrt(np.std(pp)))  \n",
    "            #  linear fit to double-log graph (gives power)  \n",
    "            m = np.polyfit(np.log10(lagvec), np.log10(tau), 1)  \n",
    "            # calculate hurst  \n",
    "            hurst = m[0]*2\n",
    "\n",
    "            return hurst \n",
    "\n",
    "        # dataframe for a historical instrument feature (ma_90 in this case). The index is the timestamps\n",
    "        # of upto lookback data points. The columns of this dataframe are the stock symbols/instrumentIds.\n",
    "        mom30Data = lookbackInstrumentFeatures.getFeatureDf('mom_30')\n",
    "        ma90Data = lookbackInstrumentFeatures.getFeatureDf('ma_90')\n",
    "\n",
    "        if len(ma90Data.index) > 20:\n",
    "            mom30 = mom30Data.iloc[-1]\n",
    "            # Go long if momentum is positive\n",
    "            predictions[mom30 > 0] = 1\n",
    "            # Go short if momentum is negative\n",
    "            predictions[mom30 <= 0] = 0          \n",
    "        else:\n",
    "            # If no sufficient data then don't take any positions\n",
    "            predictions.values[:] = 0.5\n",
    "        return predictions\n",
    "\n",
    "    def updateCount(self):\n",
    "        self.count = self.count + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Gp_ajtSC3r9t"
   },
   "source": [
    "#### Here's another example of a custom feature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "weJEUniK3r9v"
   },
   "outputs": [],
   "source": [
    "class MyCustomFeature(Feature):\n",
    "    ''''\n",
    "    Custom Feature to implement for instrument. This function would return the value of the feature you want to implement.\n",
    "    1. create a new class MyCustomFeatureClassName for the feature and implement your logic in the function computeForInstrument() -\n",
    "\n",
    "    2. modify function getCustomFeatures() to return a dictionary with Id for this class\n",
    "        (follow formats like {'my_custom_feature_identifier': MyCustomFeatureClassName}.\n",
    "        Make sure 'my_custom_feature_identifier' doesnt conflict with any of the pre defined feature Ids\n",
    "\n",
    "        def getCustomFeatures(self):\n",
    "            return {'my_custom_feature_identifier': MyCustomFeatureClassName}\n",
    "\n",
    "    3. create a dict for this feature in getInstrumentFeatureConfigDicts() above. Dict format is:\n",
    "            customFeatureDict = {'featureKey': 'my_custom_feature_key',\n",
    "                                'featureId': 'my_custom_feature_identifier',\n",
    "                                'params': {'param1': 'value1'}}\n",
    "    You can now use this feature by calling it's featureKey, 'my_custom_feature_key' in getPrediction()\n",
    "    '''\n",
    "    \n",
    "    @classmethod\n",
    "    def computeForInstrument(cls, updateNum, time, featureParams, featureKey, instrumentManager):\n",
    "        # Custom parameter which can be used as input to computation of this feature\n",
    "        param1Value = featureParams['param1']\n",
    "\n",
    "        # A holder for the all the instrument features\n",
    "        lookbackInstrumentFeatures = instrumentManager.getLookbackInstrumentFeatures()\n",
    "\n",
    "        # dataframe for a historical instrument feature (basis in this case). The index is the timestamps\n",
    "        # atmost upto lookback data points. The columns of this dataframe are the stocks/instrumentIds.\n",
    "        lookbackInstrumentValue = lookbackInstrumentFeatures.getFeatureDf('adjClose')\n",
    "\n",
    "        # The last row of the previous dataframe gives the last calculated value for that feature (basis in this case)\n",
    "        # This returns a series with stocks/instrumentIds as the index.\n",
    "        currentValue = lookbackInstrumentValue.iloc[-1]\n",
    "\n",
    "        if param1Value == 'value1':\n",
    "            return currentValue * 0.1\n",
    "        else:\n",
    "            return currentValue * 0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "FAoigNUv3r93"
   },
   "source": [
    "### Time to run the backtester!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "LMW8EmrC3r95",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing data for stock: AAPL\n",
      "20% done...\n",
      "40% done...\n",
      "60% done...\n",
      "80% done...\n",
      "Logging all the available market metrics in tensorboard\n",
      "Logging all the available instrument metrics in tensorboard\n",
      "2015-01-02 00:00:00\n",
      "2015-01-05 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-06 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-07 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-08 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-09 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-12 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-13 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-14 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-15 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-16 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-19 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-20 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-21 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-22 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-23 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-26 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-27 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-28 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-29 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-01-30 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-02-02 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "2015-02-03 00:00:00\n",
      "AAPL\n",
      "pnl: 0.00\n",
      "Position changed to: -1.00\n",
      "2015-02-04 00:00:00\n",
      "AAPL\n",
      "pnl: -0.02\n",
      "2015-02-05 00:00:00\n",
      "AAPL\n",
      "pnl: -0.85\n",
      "2015-02-06 00:00:00\n",
      "AAPL\n",
      "pnl: -1.63\n",
      "2015-02-09 00:00:00\n",
      "AAPL\n",
      "pnl: -0.70\n",
      "2015-02-10 00:00:00\n",
      "AAPL\n",
      "pnl: -1.43\n",
      "2015-02-11 00:00:00\n",
      "AAPL\n",
      "pnl: -3.54\n",
      "2015-02-12 00:00:00\n",
      "AAPL\n",
      "pnl: -6.16\n",
      "2015-02-13 00:00:00\n",
      "AAPL\n",
      "pnl: -7.61\n",
      "2015-02-16 00:00:00\n",
      "AAPL\n",
      "pnl: -8.18\n",
      "2015-02-17 00:00:00\n",
      "AAPL\n",
      "pnl: -8.18\n",
      "2015-02-18 00:00:00\n",
      "AAPL\n",
      "pnl: -8.87\n",
      "2015-02-19 00:00:00\n",
      "AAPL\n",
      "pnl: -9.69\n",
      "2015-02-20 00:00:00\n",
      "AAPL\n",
      "pnl: -9.44\n",
      "2015-02-23 00:00:00\n",
      "AAPL\n",
      "pnl: -10.40\n",
      "2015-02-24 00:00:00\n",
      "AAPL\n",
      "pnl: -13.62\n",
      "2015-02-25 00:00:00\n",
      "AAPL\n",
      "pnl: -12.85\n",
      "2015-02-26 00:00:00\n",
      "AAPL\n",
      "pnl: -9.75\n",
      "2015-02-27 00:00:00\n",
      "AAPL\n",
      "pnl: -11.25\n",
      "2015-03-02 00:00:00\n",
      "AAPL\n",
      "pnl: -9.45\n",
      "2015-03-03 00:00:00\n",
      "AAPL\n",
      "pnl: -10.03\n",
      "2015-03-04 00:00:00\n",
      "AAPL\n",
      "pnl: -10.28\n",
      "2015-03-05 00:00:00\n",
      "AAPL\n",
      "pnl: -9.52\n",
      "2015-03-06 00:00:00\n",
      "AAPL\n",
      "pnl: -7.57\n",
      "2015-03-09 00:00:00\n",
      "AAPL\n",
      "pnl: -7.74\n",
      "2015-03-10 00:00:00\n",
      "AAPL\n",
      "pnl: -8.24\n",
      "2015-03-11 00:00:00\n",
      "AAPL\n",
      "pnl: -5.82\n",
      "2015-03-12 00:00:00\n",
      "AAPL\n",
      "pnl: -3.74\n",
      "2015-03-13 00:00:00\n",
      "AAPL\n",
      "pnl: -5.77\n",
      "2015-03-16 00:00:00\n",
      "AAPL\n",
      "pnl: -4.98\n",
      "2015-03-17 00:00:00\n",
      "AAPL\n",
      "pnl: -6.23\n",
      "2015-03-18 00:00:00\n",
      "AAPL\n",
      "pnl: -8.15\n",
      "2015-03-19 00:00:00\n",
      "AAPL\n",
      "pnl: -9.46\n",
      "2015-03-20 00:00:00\n",
      "AAPL\n",
      "pnl: -8.57\n",
      "2015-03-23 00:00:00\n",
      "AAPL\n",
      "pnl: -7.10\n",
      "2015-03-24 00:00:00\n",
      "AAPL\n",
      "pnl: -8.30\n",
      "2015-03-25 00:00:00\n",
      "AAPL\n",
      "pnl: -7.83\n",
      "2015-03-26 00:00:00\n",
      "AAPL\n",
      "pnl: -4.79\n",
      "2015-03-27 00:00:00\n",
      "AAPL\n",
      "pnl: -5.58\n",
      "2015-03-30 00:00:00\n",
      "AAPL\n",
      "pnl: -4.67\n",
      "2015-03-31 00:00:00\n",
      "AAPL\n",
      "pnl: -7.53\n",
      "2015-04-01 00:00:00\n",
      "AAPL\n",
      "pnl: -5.75\n",
      "2015-04-02 00:00:00\n",
      "AAPL\n",
      "pnl: -5.59\n",
      "2015-04-03 00:00:00\n",
      "AAPL\n",
      "pnl: -6.57\n",
      "2015-04-06 00:00:00\n",
      "AAPL\n",
      "pnl: -6.57\n",
      "2015-04-07 00:00:00\n",
      "AAPL\n",
      "pnl: -8.43\n",
      "2015-04-08 00:00:00\n",
      "AAPL\n",
      "pnl: -7.20\n",
      "2015-04-09 00:00:00\n",
      "AAPL\n",
      "pnl: -6.82\n",
      "2015-04-10 00:00:00\n",
      "AAPL\n",
      "pnl: -7.71\n",
      "2015-04-13 00:00:00\n",
      "AAPL\n",
      "pnl: -8.20\n",
      "2015-04-14 00:00:00\n",
      "AAPL\n",
      "pnl: -7.97\n",
      "2015-04-15 00:00:00\n",
      "AAPL\n",
      "pnl: -7.47\n",
      "2015-04-16 00:00:00\n",
      "AAPL\n",
      "pnl: -7.91\n",
      "2015-04-17 00:00:00\n",
      "AAPL\n",
      "pnl: -7.35\n",
      "2015-04-20 00:00:00\n",
      "AAPL\n",
      "pnl: -6.04\n",
      "2015-04-21 00:00:00\n",
      "AAPL\n",
      "pnl: -8.66\n",
      "2015-04-22 00:00:00\n",
      "AAPL\n",
      "pnl: -8.03\n",
      "2015-04-23 00:00:00\n",
      "AAPL\n",
      "pnl: -9.60\n",
      "2015-04-24 00:00:00\n",
      "AAPL\n",
      "pnl: -10.56\n",
      "2015-04-27 00:00:00\n",
      "AAPL\n",
      "pnl: -11.12\n",
      "2015-04-28 00:00:00\n",
      "AAPL\n",
      "pnl: -13.30\n",
      "2015-04-29 00:00:00\n",
      "AAPL\n",
      "pnl: -11.38\n",
      "2015-04-30 00:00:00\n",
      "AAPL\n",
      "pnl: -9.61\n",
      "2015-05-01 00:00:00\n",
      "AAPL\n",
      "pnl: -6.41\n",
      "2015-05-04 00:00:00\n",
      "AAPL\n",
      "pnl: -9.90\n",
      "2015-05-05 00:00:00\n",
      "AAPL\n",
      "pnl: -9.67\n",
      "2015-05-06 00:00:00\n",
      "AAPL\n",
      "pnl: -7.01\n",
      "2015-05-07 00:00:00\n",
      "AAPL\n",
      "pnl: -6.28\n",
      "2015-05-08 00:00:00\n",
      "AAPL\n",
      "pnl: -6.99\n",
      "2015-05-11 00:00:00\n",
      "AAPL\n",
      "pnl: -9.17\n",
      "2015-05-12 00:00:00\n",
      "AAPL\n",
      "pnl: -7.97\n",
      "2015-05-13 00:00:00\n",
      "AAPL\n",
      "pnl: -7.56\n",
      "2015-05-14 00:00:00\n",
      "AAPL\n",
      "pnl: -7.68\n",
      "2015-05-15 00:00:00\n",
      "AAPL\n",
      "pnl: -10.39\n",
      "2015-05-18 00:00:00\n",
      "AAPL\n",
      "pnl: -10.23\n",
      "2015-05-19 00:00:00\n",
      "AAPL\n",
      "pnl: -11.54\n",
      "2015-05-20 00:00:00\n",
      "AAPL\n",
      "pnl: -11.43\n",
      "2015-05-21 00:00:00\n",
      "AAPL\n",
      "pnl: -11.42\n",
      "2015-05-22 00:00:00\n",
      "AAPL\n",
      "pnl: -12.64\n",
      "2015-05-25 00:00:00\n",
      "AAPL\n",
      "pnl: -13.70\n",
      "2015-05-26 00:00:00\n",
      "AAPL\n",
      "pnl: -13.70\n",
      "2015-05-27 00:00:00\n",
      "AAPL\n",
      "pnl: -11.01\n",
      "2015-05-28 00:00:00\n",
      "AAPL\n",
      "pnl: -13.24\n",
      "2015-05-29 00:00:00\n",
      "AAPL\n",
      "pnl: -13.00\n",
      "2015-06-01 00:00:00\n",
      "AAPL\n",
      "pnl: -11.62\n",
      "2015-06-02 00:00:00\n",
      "AAPL\n",
      "pnl: -11.86\n",
      "2015-06-03 00:00:00\n",
      "AAPL\n",
      "pnl: -11.32\n",
      "2015-06-04 00:00:00\n",
      "AAPL\n",
      "pnl: -11.47\n",
      "2015-06-05 00:00:00\n",
      "AAPL\n",
      "pnl: -10.77\n",
      "2015-06-08 00:00:00\n",
      "AAPL\n",
      "pnl: -10.12\n",
      "2015-06-09 00:00:00\n",
      "AAPL\n",
      "pnl: -9.33\n",
      "2015-06-10 00:00:00\n",
      "AAPL\n",
      "pnl: -8.98\n",
      "2015-06-11 00:00:00\n",
      "AAPL\n",
      "pnl: -10.33\n",
      "2015-06-12 00:00:00\n",
      "AAPL\n",
      "pnl: -10.06\n",
      "2015-06-15 00:00:00\n",
      "AAPL\n",
      "pnl: -8.75\n",
      "2015-06-16 00:00:00\n",
      "AAPL\n",
      "pnl: -8.52\n",
      "2015-06-17 00:00:00\n",
      "AAPL\n",
      "pnl: -9.15\n",
      "2015-06-18 00:00:00\n",
      "AAPL\n",
      "pnl: -8.87\n",
      "2015-06-19 00:00:00\n",
      "AAPL\n",
      "pnl: -9.41\n",
      "2015-06-22 00:00:00\n",
      "AAPL\n",
      "pnl: -8.23\n",
      "2015-06-23 00:00:00\n",
      "AAPL\n",
      "pnl: -9.16\n",
      "2015-06-24 00:00:00\n",
      "AAPL\n",
      "pnl: -8.62\n",
      "2015-06-25 00:00:00\n",
      "AAPL\n",
      "pnl: -9.62\n",
      "2015-06-26 00:00:00\n",
      "AAPL\n",
      "pnl: -9.06\n",
      "2015-06-29 00:00:00\n",
      "AAPL\n",
      "pnl: -8.37\n",
      "2015-06-30 00:00:00\n",
      "AAPL\n",
      "pnl: -6.32\n",
      "2015-07-01 00:00:00\n",
      "AAPL\n",
      "pnl: -7.15\n",
      "2015-07-02 00:00:00\n",
      "AAPL\n",
      "pnl: -8.23\n",
      "2015-07-03 00:00:00\n",
      "AAPL\n",
      "pnl: -8.08\n",
      "2015-07-06 00:00:00\n",
      "AAPL\n",
      "pnl: -8.08\n",
      "2015-07-07 00:00:00\n",
      "AAPL\n",
      "pnl: -7.68\n",
      "2015-07-08 00:00:00\n",
      "AAPL\n",
      "pnl: -7.39\n",
      "2015-07-09 00:00:00\n",
      "AAPL\n",
      "pnl: -4.51\n",
      "2015-07-10 00:00:00\n",
      "AAPL\n",
      "pnl: -2.21\n",
      "2015-07-13 00:00:00\n",
      "AAPL\n",
      "pnl: -5.17\n",
      "2015-07-14 00:00:00\n",
      "AAPL\n",
      "pnl: -7.36\n",
      "2015-07-15 00:00:00\n",
      "AAPL\n",
      "pnl: -7.32\n",
      "2015-07-16 00:00:00\n",
      "AAPL\n",
      "pnl: -8.43\n",
      "2015-07-17 00:00:00\n",
      "AAPL\n",
      "pnl: -9.99\n",
      "2015-07-20 00:00:00\n",
      "AAPL\n",
      "pnl: -11.01\n",
      "2015-07-21 00:00:00\n",
      "AAPL\n",
      "pnl: -13.27\n",
      "2015-07-22 00:00:00\n",
      "AAPL\n",
      "pnl: -12.05\n",
      "2015-07-23 00:00:00\n",
      "AAPL\n",
      "pnl: -6.96\n",
      "2015-07-24 00:00:00\n",
      "AAPL\n",
      "pnl: -6.90\n",
      "2015-07-27 00:00:00\n",
      "AAPL\n",
      "pnl: -6.29\n",
      "2015-07-28 00:00:00\n",
      "AAPL\n",
      "pnl: -4.70\n",
      "2015-07-29 00:00:00\n",
      "AAPL\n",
      "pnl: -5.26\n",
      "2015-07-30 00:00:00\n",
      "AAPL\n",
      "pnl: -4.90\n",
      "2015-07-31 00:00:00\n",
      "AAPL\n",
      "pnl: -4.33\n",
      "2015-08-03 00:00:00\n",
      "AAPL\n",
      "pnl: -3.34\n",
      "2015-08-04 00:00:00\n",
      "AAPL\n",
      "pnl: -0.71\n",
      "2015-08-05 00:00:00\n",
      "AAPL\n",
      "pnl: 2.79\n",
      "2015-08-06 00:00:00\n",
      "AAPL\n",
      "pnl: 2.09\n",
      "2015-08-07 00:00:00\n",
      "AAPL\n",
      "pnl: 1.86\n",
      "2015-08-10 00:00:00\n",
      "AAPL\n",
      "pnl: 1.50\n",
      "2015-08-11 00:00:00\n",
      "AAPL\n",
      "pnl: -2.39\n",
      "2015-08-12 00:00:00\n",
      "AAPL\n",
      "pnl: 3.38\n",
      "2015-08-13 00:00:00\n",
      "AAPL\n",
      "pnl: 1.76\n",
      "2015-08-14 00:00:00\n",
      "AAPL\n",
      "pnl: 1.84\n",
      "2015-08-17 00:00:00\n",
      "AAPL\n",
      "pnl: 1.09\n",
      "2015-08-18 00:00:00\n",
      "AAPL\n",
      "pnl: -0.02\n",
      "2015-08-19 00:00:00\n",
      "AAPL\n",
      "pnl: 0.59\n",
      "2015-08-20 00:00:00\n",
      "AAPL\n",
      "pnl: 1.97\n",
      "2015-08-21 00:00:00\n",
      "AAPL\n",
      "pnl: 4.16\n",
      "2015-08-24 00:00:00\n",
      "AAPL\n",
      "pnl: 10.54\n",
      "2015-08-25 00:00:00\n",
      "AAPL\n",
      "pnl: 12.98\n",
      "2015-08-26 00:00:00\n",
      "AAPL\n",
      "pnl: 12.41\n",
      "2015-08-27 00:00:00\n",
      "AAPL\n",
      "pnl: 6.90\n",
      "2015-08-28 00:00:00\n",
      "AAPL\n",
      "pnl: 3.91\n",
      "2015-08-31 00:00:00\n",
      "AAPL\n",
      "pnl: 3.57\n",
      "2015-09-01 00:00:00\n",
      "AAPL\n",
      "pnl: 4.06\n",
      "2015-09-02 00:00:00\n",
      "AAPL\n",
      "pnl: 8.72\n",
      "2015-09-03 00:00:00\n",
      "AAPL\n",
      "pnl: 4.45\n",
      "2015-09-04 00:00:00\n",
      "AAPL\n",
      "pnl: 6.27\n",
      "2015-09-07 00:00:00\n",
      "AAPL\n",
      "pnl: 7.29\n",
      "2015-09-08 00:00:00\n",
      "AAPL\n",
      "pnl: 7.29\n",
      "2015-09-09 00:00:00\n",
      "AAPL\n",
      "pnl: 4.47\n",
      "2015-09-10 00:00:00\n",
      "AAPL\n",
      "pnl: 6.47\n",
      "2015-09-11 00:00:00\n",
      "AAPL\n",
      "pnl: 4.23\n",
      "2015-09-14 00:00:00\n",
      "AAPL\n",
      "pnl: 2.71\n",
      "2015-09-15 00:00:00\n",
      "AAPL\n",
      "pnl: 1.70\n",
      "2015-09-16 00:00:00\n",
      "AAPL\n",
      "pnl: 0.80\n",
      "2015-09-17 00:00:00\n",
      "AAPL\n",
      "pnl: 0.68\n",
      "2015-09-18 00:00:00\n",
      "AAPL\n",
      "pnl: 2.98\n",
      "2015-09-21 00:00:00\n",
      "AAPL\n",
      "pnl: 3.42\n",
      "2015-09-22 00:00:00\n",
      "AAPL\n",
      "pnl: 1.79\n",
      "2015-09-23 00:00:00\n",
      "AAPL\n",
      "pnl: 3.46\n",
      "2015-09-24 00:00:00\n",
      "AAPL\n",
      "pnl: 2.61\n",
      "2015-09-25 00:00:00\n",
      "AAPL\n",
      "pnl: 1.98\n",
      "2015-09-28 00:00:00\n",
      "AAPL\n",
      "pnl: 2.25\n",
      "2015-09-29 00:00:00\n",
      "AAPL\n",
      "pnl: 4.35\n",
      "2015-09-30 00:00:00\n",
      "AAPL\n",
      "pnl: 7.48\n",
      "2015-10-01 00:00:00\n",
      "AAPL\n",
      "pnl: 6.33\n",
      "2015-10-02 00:00:00\n",
      "AAPL\n",
      "pnl: 7.00\n",
      "2015-10-05 00:00:00\n",
      "AAPL\n",
      "pnl: 6.26\n",
      "2015-10-06 00:00:00\n",
      "AAPL\n",
      "pnl: 5.89\n",
      "2015-10-07 00:00:00\n",
      "AAPL\n",
      "pnl: 5.40\n",
      "2015-10-08 00:00:00\n",
      "AAPL\n",
      "pnl: 5.89\n",
      "2015-10-09 00:00:00\n",
      "AAPL\n",
      "pnl: 7.07\n",
      "2015-10-12 00:00:00\n",
      "AAPL\n",
      "pnl: 4.65\n",
      "2015-10-13 00:00:00\n",
      "AAPL\n",
      "pnl: 5.13\n",
      "2015-10-14 00:00:00\n",
      "AAPL\n",
      "pnl: 4.95\n",
      "2015-10-15 00:00:00\n",
      "AAPL\n",
      "pnl: 6.42\n",
      "2015-10-16 00:00:00\n",
      "AAPL\n",
      "pnl: 4.89\n",
      "2015-10-19 00:00:00\n",
      "AAPL\n",
      "pnl: 5.65\n",
      "2015-10-20 00:00:00\n",
      "AAPL\n",
      "pnl: 5.01\n",
      "2015-10-21 00:00:00\n",
      "AAPL\n",
      "pnl: 3.12\n",
      "2015-10-22 00:00:00\n",
      "AAPL\n",
      "pnl: 3.13\n",
      "2015-10-23 00:00:00\n",
      "AAPL\n",
      "pnl: 1.52\n",
      "2015-10-26 00:00:00\n",
      "AAPL\n",
      "pnl: -1.79\n",
      "2015-10-27 00:00:00\n",
      "AAPL\n",
      "pnl: 1.72\n",
      "2015-10-28 00:00:00\n",
      "AAPL\n",
      "pnl: 2.40\n",
      "2015-10-29 00:00:00\n",
      "AAPL\n",
      "pnl: -1.97\n",
      "2015-10-30 00:00:00\n",
      "AAPL\n",
      "pnl: -3.14\n",
      "2015-11-02 00:00:00\n",
      "AAPL\n",
      "pnl: -2.18\n",
      "2015-11-03 00:00:00\n",
      "AAPL\n",
      "pnl: -3.74\n",
      "2015-11-04 00:00:00\n",
      "AAPL\n",
      "pnl: -5.03\n",
      "2015-11-05 00:00:00\n",
      "AAPL\n",
      "pnl: -4.50\n",
      "2015-11-06 00:00:00\n",
      "AAPL\n",
      "pnl: -3.98\n",
      "2015-11-09 00:00:00\n",
      "AAPL\n",
      "pnl: -4.11\n",
      "2015-11-10 00:00:00\n",
      "AAPL\n",
      "pnl: -3.65\n",
      "2015-11-11 00:00:00\n",
      "AAPL\n",
      "pnl: -0.12\n",
      "2015-11-12 00:00:00\n",
      "AAPL\n",
      "pnl: 0.50\n",
      "2015-11-13 00:00:00\n",
      "AAPL\n",
      "pnl: 0.86\n",
      "2015-11-16 00:00:00\n",
      "AAPL\n",
      "pnl: 4.00\n",
      "2015-11-17 00:00:00\n",
      "AAPL\n",
      "pnl: 2.29\n",
      "2015-11-18 00:00:00\n",
      "AAPL\n",
      "pnl: 2.75\n",
      "2015-11-19 00:00:00\n",
      "AAPL\n",
      "pnl: -0.60\n",
      "2015-11-20 00:00:00\n",
      "AAPL\n",
      "pnl: -1.99\n",
      "2015-11-23 00:00:00\n",
      "AAPL\n",
      "pnl: -2.47\n",
      "2015-11-24 00:00:00\n",
      "AAPL\n",
      "pnl: -1.03\n",
      "2015-11-25 00:00:00\n",
      "AAPL\n",
      "pnl: -2.08\n",
      "2015-11-26 00:00:00\n",
      "AAPL\n",
      "pnl: -1.29\n",
      "2015-11-27 00:00:00\n",
      "AAPL\n",
      "pnl: -1.29\n",
      "2015-11-30 00:00:00\n",
      "AAPL\n",
      "pnl: -1.09\n",
      "2015-12-01 00:00:00\n",
      "AAPL\n",
      "pnl: -1.54\n",
      "2015-12-02 00:00:00\n",
      "AAPL\n",
      "pnl: -0.65\n",
      "2015-12-03 00:00:00\n",
      "AAPL\n",
      "pnl: 0.34\n",
      "2015-12-04 00:00:00\n",
      "AAPL\n",
      "pnl: 1.34\n",
      "2015-12-07 00:00:00\n",
      "AAPL\n",
      "pnl: -2.22\n",
      "2015-12-08 00:00:00\n",
      "AAPL\n",
      "pnl: -1.52\n",
      "2015-12-09 00:00:00\n",
      "AAPL\n",
      "pnl: -1.48\n",
      "2015-12-10 00:00:00\n",
      "AAPL\n",
      "pnl: 0.95\n",
      "2015-12-11 00:00:00\n",
      "AAPL\n",
      "pnl: 0.44\n",
      "2015-12-14 00:00:00\n",
      "AAPL\n",
      "pnl: 3.22\n",
      "2015-12-15 00:00:00\n",
      "AAPL\n",
      "pnl: 3.87\n",
      "2015-12-16 00:00:00\n",
      "AAPL\n",
      "pnl: 5.72\n",
      "2015-12-17 00:00:00\n",
      "AAPL\n",
      "pnl: 4.93\n",
      "2015-12-18 00:00:00\n",
      "AAPL\n",
      "pnl: 7.12\n",
      "2015-12-21 00:00:00\n",
      "AAPL\n",
      "pnl: 9.87\n",
      "2015-12-22 00:00:00\n",
      "AAPL\n",
      "pnl: 8.66\n",
      "2015-12-23 00:00:00\n",
      "AAPL\n",
      "pnl: 8.75\n",
      "2015-12-24 00:00:00\n",
      "AAPL\n",
      "pnl: 7.47\n",
      "2015-12-25 00:00:00\n",
      "AAPL\n",
      "pnl: 8.01\n",
      "2015-12-28 00:00:00\n",
      "AAPL\n",
      "pnl: 8.01\n",
      "2015-12-29 00:00:00\n",
      "AAPL\n",
      "pnl: 9.13\n",
      "2015-12-30 00:00:00\n",
      "AAPL\n",
      "pnl: 7.35\n",
      "2015-12-31 00:00:00\n",
      "AAPL\n",
      "pnl: 8.67\n",
      "2016-01-01 00:00:00\n",
      "AAPL\n",
      "pnl: 10.58\n",
      "2016-01-04 00:00:00\n",
      "AAPL\n",
      "pnl: 10.58\n",
      "2016-01-05 00:00:00\n",
      "AAPL\n",
      "pnl: 10.50\n",
      "2016-01-06 00:00:00\n",
      "AAPL\n",
      "pnl: 12.95\n",
      "2016-01-07 00:00:00\n",
      "AAPL\n",
      "pnl: 14.82\n",
      "2016-01-08 00:00:00\n",
      "AAPL\n",
      "pnl: 18.77\n",
      "2016-01-11 00:00:00\n",
      "AAPL\n",
      "pnl: 18.30\n",
      "2016-01-12 00:00:00\n",
      "AAPL\n",
      "pnl: 16.84\n",
      "2016-01-13 00:00:00\n",
      "AAPL\n",
      "pnl: 15.51\n",
      "2016-01-14 00:00:00\n",
      "AAPL\n",
      "pnl: 17.90\n",
      "2016-01-15 00:00:00\n",
      "AAPL\n",
      "pnl: 15.92\n",
      "2016-01-18 00:00:00\n",
      "AAPL\n",
      "pnl: 18.14\n",
      "2016-01-19 00:00:00\n",
      "AAPL\n",
      "pnl: 18.14\n",
      "2016-01-20 00:00:00\n",
      "AAPL\n",
      "pnl: 18.58\n",
      "2016-01-21 00:00:00\n",
      "AAPL\n",
      "pnl: 18.46\n",
      "2016-01-22 00:00:00\n",
      "AAPL\n",
      "pnl: 18.91\n",
      "2016-01-25 00:00:00\n",
      "AAPL\n",
      "pnl: 14.15\n",
      "2016-01-26 00:00:00\n",
      "AAPL\n",
      "pnl: 15.99\n",
      "2016-01-27 00:00:00\n",
      "AAPL\n",
      "pnl: 15.48\n",
      "2016-01-28 00:00:00\n",
      "AAPL\n",
      "pnl: 21.59\n",
      "2016-01-29 00:00:00\n",
      "AAPL\n",
      "pnl: 20.97\n",
      "2016-02-01 00:00:00\n",
      "AAPL\n",
      "pnl: 17.95\n",
      "2016-02-02 00:00:00\n",
      "AAPL\n",
      "pnl: 18.79\n",
      "2016-02-03 00:00:00\n",
      "AAPL\n",
      "pnl: 20.61\n",
      "2016-02-04 00:00:00\n",
      "AAPL\n",
      "pnl: 18.87\n",
      "2016-02-05 00:00:00\n",
      "AAPL\n",
      "pnl: 18.15\n",
      "2016-02-08 00:00:00\n",
      "AAPL\n",
      "pnl: 20.56\n",
      "2016-02-09 00:00:00\n",
      "AAPL\n",
      "pnl: 19.63\n",
      "2016-02-10 00:00:00\n",
      "AAPL\n",
      "pnl: 19.65\n",
      "2016-02-11 00:00:00\n",
      "AAPL\n",
      "pnl: 20.33\n",
      "2016-02-12 00:00:00\n",
      "AAPL\n",
      "pnl: 20.86\n",
      "2016-02-15 00:00:00\n",
      "AAPL\n",
      "pnl: 20.59\n",
      "2016-02-16 00:00:00\n",
      "AAPL\n",
      "pnl: 20.59\n",
      "2016-02-17 00:00:00\n",
      "AAPL\n",
      "pnl: 18.11\n",
      "2016-02-18 00:00:00\n",
      "AAPL\n",
      "pnl: 16.73\n",
      "2016-02-19 00:00:00\n",
      "AAPL\n",
      "pnl: 18.46\n",
      "2016-02-22 00:00:00\n",
      "AAPL\n",
      "pnl: 18.67\n",
      "2016-02-23 00:00:00\n",
      "AAPL\n",
      "pnl: 17.89\n",
      "2016-02-24 00:00:00\n",
      "AAPL\n",
      "pnl: 19.93\n",
      "2016-02-25 00:00:00\n",
      "AAPL\n",
      "pnl: 18.61\n",
      "2016-02-26 00:00:00\n",
      "AAPL\n",
      "pnl: 18.00\n",
      "2016-02-29 00:00:00\n",
      "AAPL\n",
      "pnl: 17.86\n",
      "2016-03-01 00:00:00\n",
      "AAPL\n",
      "pnl: 18.06\n",
      "2016-03-02 00:00:00\n",
      "AAPL\n",
      "pnl: 14.47\n",
      "2016-03-03 00:00:00\n",
      "AAPL\n",
      "pnl: 14.27\n",
      "2016-03-04 00:00:00\n",
      "AAPL\n",
      "pnl: 13.57\n",
      "2016-03-07 00:00:00\n",
      "AAPL\n",
      "pnl: 12.16\n",
      "2016-03-08 00:00:00\n",
      "AAPL\n",
      "pnl: 13.22\n",
      "2016-03-09 00:00:00\n",
      "AAPL\n",
      "pnl: 14.01\n",
      "2016-03-10 00:00:00\n",
      "AAPL\n",
      "pnl: 13.92\n",
      "2016-03-11 00:00:00\n",
      "AAPL\n",
      "pnl: 13.88\n",
      "2016-03-14 00:00:00\n",
      "AAPL\n",
      "pnl: 12.86\n",
      "2016-03-15 00:00:00\n",
      "AAPL\n",
      "pnl: 12.61\n",
      "2016-03-16 00:00:00\n",
      "AAPL\n",
      "pnl: 10.69\n",
      "2016-03-17 00:00:00\n",
      "AAPL\n",
      "pnl: 9.39\n",
      "2016-03-18 00:00:00\n",
      "AAPL\n",
      "pnl: 9.55\n",
      "2016-03-21 00:00:00\n",
      "AAPL\n",
      "pnl: 9.43\n",
      "2016-03-22 00:00:00\n",
      "AAPL\n",
      "pnl: 9.44\n",
      "2016-03-23 00:00:00\n",
      "AAPL\n",
      "pnl: 8.69\n",
      "2016-03-24 00:00:00\n",
      "AAPL\n",
      "pnl: 9.24\n",
      "2016-03-25 00:00:00\n",
      "AAPL\n",
      "pnl: 9.67\n",
      "2016-03-28 00:00:00\n",
      "AAPL\n",
      "pnl: 9.67\n",
      "2016-03-29 00:00:00\n",
      "AAPL\n",
      "pnl: 10.12\n",
      "2016-03-30 00:00:00\n",
      "AAPL\n",
      "pnl: 7.79\n",
      "2016-03-31 00:00:00\n",
      "AAPL\n",
      "pnl: 6.03\n",
      "2016-04-01 00:00:00\n",
      "AAPL\n",
      "pnl: 6.57\n",
      "2016-04-04 00:00:00\n",
      "AAPL\n",
      "pnl: 5.63\n",
      "2016-04-05 00:00:00\n",
      "AAPL\n",
      "pnl: 4.57\n",
      "2016-04-06 00:00:00\n",
      "AAPL\n",
      "pnl: 5.80\n",
      "2016-04-07 00:00:00\n",
      "AAPL\n",
      "pnl: 4.72\n",
      "2016-04-08 00:00:00\n",
      "AAPL\n",
      "pnl: 6.99\n",
      "2016-04-11 00:00:00\n",
      "AAPL\n",
      "pnl: 6.87\n",
      "2016-04-12 00:00:00\n",
      "AAPL\n",
      "pnl: 6.54\n",
      "2016-04-13 00:00:00\n",
      "AAPL\n",
      "pnl: 5.21\n",
      "2016-04-14 00:00:00\n",
      "AAPL\n",
      "pnl: 3.71\n",
      "2016-04-15 00:00:00\n",
      "AAPL\n",
      "pnl: 3.66\n",
      "2016-04-18 00:00:00\n",
      "AAPL\n",
      "pnl: 5.76\n",
      "2016-04-19 00:00:00\n",
      "AAPL\n",
      "pnl: 7.98\n",
      "2016-04-20 00:00:00\n",
      "AAPL\n",
      "pnl: 8.51\n",
      "2016-04-21 00:00:00\n",
      "AAPL\n",
      "pnl: 8.30\n",
      "2016-04-22 00:00:00\n",
      "AAPL\n",
      "pnl: 9.39\n",
      "2016-04-25 00:00:00\n",
      "AAPL\n",
      "pnl: 9.66\n",
      "2016-04-26 00:00:00\n",
      "AAPL\n",
      "pnl: 10.22\n",
      "2016-04-27 00:00:00\n",
      "AAPL\n",
      "pnl: 10.90\n",
      "2016-04-28 00:00:00\n",
      "AAPL\n",
      "pnl: 17.01\n",
      "2016-04-29 00:00:00\n",
      "AAPL\n",
      "pnl: 19.80\n",
      "2016-05-02 00:00:00\n",
      "AAPL\n",
      "pnl: 20.82\n",
      "2016-05-03 00:00:00\n",
      "AAPL\n",
      "pnl: 20.91\n",
      "2016-05-04 00:00:00\n",
      "AAPL\n",
      "pnl: 19.47\n",
      "2016-05-05 00:00:00\n",
      "AAPL\n",
      "pnl: 20.40\n",
      "2016-05-06 00:00:00\n",
      "AAPL\n",
      "pnl: 20.76\n",
      "2016-05-09 00:00:00\n",
      "AAPL\n",
      "pnl: 21.25\n",
      "2016-05-10 00:00:00\n",
      "AAPL\n",
      "pnl: 21.18\n",
      "2016-05-11 00:00:00\n",
      "AAPL\n",
      "pnl: 20.59\n",
      "2016-05-12 00:00:00\n",
      "AAPL\n",
      "pnl: 21.44\n",
      "2016-05-13 00:00:00\n",
      "AAPL\n",
      "pnl: 23.48\n",
      "2016-05-16 00:00:00\n",
      "AAPL\n",
      "pnl: 23.32\n",
      "2016-05-17 00:00:00\n",
      "AAPL\n",
      "pnl: 20.16\n",
      "2016-05-18 00:00:00\n",
      "AAPL\n",
      "pnl: 20.52\n",
      "2016-05-19 00:00:00\n",
      "AAPL\n",
      "pnl: 19.52\n",
      "2016-05-20 00:00:00\n",
      "AAPL\n",
      "pnl: 19.85\n",
      "2016-05-23 00:00:00\n",
      "AAPL\n",
      "pnl: 18.90\n",
      "2016-05-24 00:00:00\n",
      "AAPL\n",
      "pnl: 17.76\n",
      "2016-05-25 00:00:00\n",
      "AAPL\n",
      "pnl: 16.37\n",
      "2016-05-26 00:00:00\n",
      "AAPL\n",
      "pnl: 14.76\n",
      "2016-05-27 00:00:00\n",
      "AAPL\n",
      "pnl: 14.01\n",
      "2016-05-30 00:00:00\n",
      "AAPL\n",
      "pnl: 14.07\n",
      "2016-05-31 00:00:00\n",
      "AAPL\n",
      "pnl: 14.07\n",
      "2016-06-01 00:00:00\n",
      "AAPL\n",
      "pnl: 14.53\n",
      "2016-06-02 00:00:00\n",
      "AAPL\n",
      "pnl: 15.85\n",
      "2016-06-03 00:00:00\n",
      "AAPL\n",
      "pnl: 16.54\n",
      "2016-06-06 00:00:00\n",
      "AAPL\n",
      "pnl: 16.36\n",
      "2016-06-07 00:00:00\n",
      "AAPL\n",
      "pnl: 15.69\n",
      "2016-06-08 00:00:00\n",
      "AAPL\n",
      "pnl: 15.31\n",
      "2016-06-09 00:00:00\n",
      "AAPL\n",
      "pnl: 15.40\n",
      "2016-06-10 00:00:00\n",
      "AAPL\n",
      "pnl: 14.73\n",
      "2016-06-13 00:00:00\n",
      "AAPL\n",
      "pnl: 15.50\n",
      "2016-06-14 00:00:00\n",
      "AAPL\n",
      "pnl: 16.90\n",
      "2016-06-15 00:00:00\n",
      "AAPL\n",
      "pnl: 16.79\n",
      "2016-06-16 00:00:00\n",
      "AAPL\n",
      "pnl: 17.09\n",
      "2016-06-17 00:00:00\n",
      "AAPL\n",
      "pnl: 16.70\n",
      "2016-06-20 00:00:00\n",
      "AAPL\n",
      "pnl: 18.79\n",
      "2016-06-21 00:00:00\n",
      "AAPL\n",
      "pnl: 19.01\n",
      "2016-06-22 00:00:00\n",
      "AAPL\n",
      "pnl: 18.25\n",
      "2016-06-23 00:00:00\n",
      "AAPL\n",
      "pnl: 18.58\n",
      "2016-06-24 00:00:00\n",
      "AAPL\n",
      "pnl: 18.07\n",
      "2016-06-27 00:00:00\n",
      "AAPL\n",
      "pnl: 20.61\n",
      "2016-06-28 00:00:00\n",
      "AAPL\n",
      "pnl: 21.89\n",
      "2016-06-29 00:00:00\n",
      "AAPL\n",
      "pnl: 20.43\n",
      "2016-06-30 00:00:00\n",
      "AAPL\n",
      "pnl: 19.67\n",
      "2016-07-01 00:00:00\n",
      "AAPL\n",
      "pnl: 18.54\n",
      "2016-07-04 00:00:00\n",
      "AAPL\n",
      "pnl: 18.27\n",
      "2016-07-05 00:00:00\n",
      "AAPL\n",
      "pnl: 18.27\n",
      "2016-07-06 00:00:00\n",
      "AAPL\n",
      "pnl: 19.11\n",
      "2016-07-07 00:00:00\n",
      "AAPL\n",
      "pnl: 18.60\n",
      "2016-07-08 00:00:00\n",
      "AAPL\n",
      "pnl: 18.22\n",
      "2016-07-11 00:00:00\n",
      "AAPL\n",
      "pnl: 17.52\n",
      "2016-07-12 00:00:00\n",
      "AAPL\n",
      "pnl: 17.24\n",
      "2016-07-13 00:00:00\n",
      "AAPL\n",
      "pnl: 16.83\n",
      "2016-07-14 00:00:00\n",
      "AAPL\n",
      "pnl: 17.34\n",
      "2016-07-15 00:00:00\n",
      "AAPL\n",
      "pnl: 15.54\n",
      "2016-07-18 00:00:00\n",
      "AAPL\n",
      "pnl: 15.55\n",
      "2016-07-19 00:00:00\n",
      "AAPL\n",
      "pnl: 14.56\n",
      "2016-07-20 00:00:00\n",
      "AAPL\n",
      "pnl: 14.52\n",
      "2016-07-21 00:00:00\n",
      "AAPL\n",
      "pnl: 14.44\n",
      "2016-07-22 00:00:00\n",
      "AAPL\n",
      "pnl: 14.94\n",
      "2016-07-25 00:00:00\n",
      "AAPL\n",
      "pnl: 15.66\n",
      "2016-07-26 00:00:00\n",
      "AAPL\n",
      "pnl: 16.90\n",
      "2016-07-27 00:00:00\n",
      "AAPL\n",
      "pnl: 17.53\n",
      "2016-07-28 00:00:00\n",
      "AAPL\n",
      "pnl: 11.63\n",
      "2016-07-29 00:00:00\n",
      "AAPL\n",
      "pnl: 10.32\n",
      "2016-08-01 00:00:00\n",
      "AAPL\n",
      "pnl: 10.44\n",
      "2016-08-02 00:00:00\n",
      "AAPL\n",
      "pnl: 8.71\n",
      "2016-08-03 00:00:00\n",
      "AAPL\n",
      "pnl: 10.19\n",
      "2016-08-04 00:00:00\n",
      "AAPL\n",
      "pnl: 8.95\n",
      "2016-08-05 00:00:00\n",
      "AAPL\n",
      "pnl: 8.34\n",
      "2016-08-08 00:00:00\n",
      "AAPL\n",
      "pnl: 6.82\n",
      "2016-08-09 00:00:00\n",
      "AAPL\n",
      "pnl: 5.98\n",
      "2016-08-10 00:00:00\n",
      "AAPL\n",
      "pnl: 5.56\n",
      "2016-08-11 00:00:00\n",
      "AAPL\n",
      "pnl: 6.33\n",
      "2016-08-12 00:00:00\n",
      "AAPL\n",
      "pnl: 6.39\n",
      "2016-08-15 00:00:00\n",
      "AAPL\n",
      "pnl: 6.16\n",
      "2016-08-16 00:00:00\n",
      "AAPL\n",
      "pnl: 4.93\n",
      "2016-08-17 00:00:00\n",
      "AAPL\n",
      "pnl: 5.02\n",
      "2016-08-18 00:00:00\n",
      "AAPL\n",
      "pnl: 5.17\n",
      "2016-08-19 00:00:00\n",
      "AAPL\n",
      "pnl: 5.30\n",
      "2016-08-22 00:00:00\n",
      "AAPL\n",
      "pnl: 5.04\n",
      "2016-08-23 00:00:00\n",
      "AAPL\n",
      "pnl: 5.84\n",
      "2016-08-24 00:00:00\n",
      "AAPL\n",
      "pnl: 5.52\n",
      "2016-08-25 00:00:00\n",
      "AAPL\n",
      "pnl: 6.30\n",
      "2016-08-26 00:00:00\n",
      "AAPL\n",
      "pnl: 6.73\n",
      "2016-08-29 00:00:00\n",
      "AAPL\n",
      "pnl: 7.33\n",
      "2016-08-30 00:00:00\n",
      "AAPL\n",
      "pnl: 7.44\n",
      "2016-08-31 00:00:00\n",
      "AAPL\n",
      "pnl: 8.22\n",
      "2016-09-01 00:00:00\n",
      "AAPL\n",
      "pnl: 8.12\n",
      "2016-09-02 00:00:00\n",
      "AAPL\n",
      "pnl: 7.53\n",
      "2016-09-05 00:00:00\n",
      "AAPL\n",
      "pnl: 6.58\n",
      "2016-09-06 00:00:00\n",
      "AAPL\n",
      "pnl: 6.58\n",
      "2016-09-07 00:00:00\n",
      "AAPL\n",
      "pnl: 6.61\n",
      "2016-09-08 00:00:00\n",
      "AAPL\n",
      "pnl: 5.99\n",
      "2016-09-09 00:00:00\n",
      "AAPL\n",
      "pnl: 8.67\n",
      "2016-09-12 00:00:00\n",
      "AAPL\n",
      "pnl: 10.93\n",
      "2016-09-13 00:00:00\n",
      "AAPL\n",
      "pnl: 8.75\n",
      "2016-09-14 00:00:00\n",
      "AAPL\n",
      "pnl: 6.37\n",
      "2016-09-15 00:00:00\n",
      "AAPL\n",
      "pnl: 2.76\n",
      "2016-09-16 00:00:00\n",
      "AAPL\n",
      "pnl: -0.83\n",
      "2016-09-19 00:00:00\n",
      "AAPL\n",
      "pnl: -0.22\n",
      "2016-09-20 00:00:00\n",
      "AAPL\n",
      "pnl: 1.05\n",
      "2016-09-21 00:00:00\n",
      "AAPL\n",
      "pnl: 1.06\n",
      "2016-09-22 00:00:00\n",
      "AAPL\n",
      "pnl: 1.08\n",
      "2016-09-23 00:00:00\n",
      "AAPL\n",
      "pnl: 0.07\n",
      "2016-09-26 00:00:00\n",
      "AAPL\n",
      "pnl: 1.87\n",
      "2016-09-27 00:00:00\n",
      "AAPL\n",
      "pnl: 1.71\n",
      "2016-09-28 00:00:00\n",
      "AAPL\n",
      "pnl: 1.51\n",
      "2016-09-29 00:00:00\n",
      "AAPL\n",
      "pnl: 0.70\n",
      "2016-09-30 00:00:00\n",
      "AAPL\n",
      "pnl: 2.37\n",
      "2016-10-03 00:00:00\n",
      "AAPL\n",
      "pnl: 1.55\n",
      "2016-10-04 00:00:00\n",
      "AAPL\n",
      "pnl: 2.05\n",
      "2016-10-05 00:00:00\n",
      "AAPL\n",
      "pnl: 1.60\n",
      "2016-10-06 00:00:00\n",
      "AAPL\n",
      "pnl: 1.55\n",
      "2016-10-07 00:00:00\n",
      "AAPL\n",
      "pnl: 0.76\n",
      "2016-10-10 00:00:00\n",
      "AAPL\n",
      "pnl: 0.60\n",
      "2016-10-11 00:00:00\n",
      "AAPL\n",
      "pnl: -1.29\n",
      "2016-10-12 00:00:00\n",
      "AAPL\n",
      "pnl: -1.52\n",
      "2016-10-13 00:00:00\n",
      "AAPL\n",
      "pnl: -2.51\n",
      "2016-10-14 00:00:00\n",
      "AAPL\n",
      "pnl: -2.17\n",
      "2016-10-17 00:00:00\n",
      "AAPL\n",
      "pnl: -2.78\n",
      "2016-10-18 00:00:00\n",
      "AAPL\n",
      "pnl: -2.70\n",
      "2016-10-19 00:00:00\n",
      "AAPL\n",
      "pnl: -2.63\n",
      "2016-10-20 00:00:00\n",
      "AAPL\n",
      "pnl: -2.30\n",
      "2016-10-21 00:00:00\n",
      "AAPL\n",
      "pnl: -2.24\n",
      "2016-10-24 00:00:00\n",
      "AAPL\n",
      "pnl: -1.81\n",
      "2016-10-25 00:00:00\n",
      "AAPL\n",
      "pnl: -2.80\n",
      "2016-10-26 00:00:00\n",
      "AAPL\n",
      "pnl: -3.37\n",
      "2016-10-27 00:00:00\n",
      "AAPL\n",
      "pnl: -0.85\n",
      "2016-10-28 00:00:00\n",
      "AAPL\n",
      "pnl: 0.20\n",
      "2016-10-31 00:00:00\n",
      "AAPL\n",
      "pnl: 0.92\n",
      "2016-11-01 00:00:00\n",
      "AAPL\n",
      "pnl: 1.09\n",
      "2016-11-02 00:00:00\n",
      "AAPL\n",
      "pnl: 3.03\n",
      "2016-11-03 00:00:00\n",
      "AAPL\n",
      "pnl: 2.93\n",
      "2016-11-04 00:00:00\n",
      "AAPL\n",
      "pnl: 4.06\n",
      "2016-11-07 00:00:00\n",
      "AAPL\n",
      "pnl: 5.00\n",
      "2016-11-08 00:00:00\n",
      "AAPL\n",
      "pnl: 3.51\n",
      "2016-11-09 00:00:00\n",
      "AAPL\n",
      "pnl: 2.89\n",
      "2016-11-10 00:00:00\n",
      "AAPL\n",
      "pnl: 3.06\n",
      "2016-11-11 00:00:00\n",
      "AAPL\n",
      "pnl: 6.00\n",
      "2016-11-14 00:00:00\n",
      "AAPL\n",
      "pnl: 5.39\n",
      "2016-11-15 00:00:00\n",
      "AAPL\n",
      "pnl: 7.98\n",
      "2016-11-16 00:00:00\n",
      "AAPL\n",
      "pnl: 6.65\n",
      "2016-11-17 00:00:00\n",
      "AAPL\n",
      "pnl: 3.91\n",
      "2016-11-18 00:00:00\n",
      "AAPL\n",
      "pnl: 3.95\n",
      "2016-11-21 00:00:00\n",
      "AAPL\n",
      "pnl: 3.84\n",
      "2016-11-22 00:00:00\n",
      "AAPL\n",
      "pnl: 2.26\n",
      "2016-11-23 00:00:00\n",
      "AAPL\n",
      "pnl: 2.19\n",
      "2016-11-24 00:00:00\n",
      "AAPL\n",
      "pnl: 2.73\n",
      "2016-11-25 00:00:00\n",
      "AAPL\n",
      "pnl: 2.73\n",
      "2016-11-28 00:00:00\n",
      "AAPL\n",
      "pnl: 2.20\n",
      "2016-11-29 00:00:00\n",
      "AAPL\n",
      "pnl: 2.41\n",
      "2016-11-30 00:00:00\n",
      "AAPL\n",
      "pnl: 2.51\n",
      "2016-12-01 00:00:00\n",
      "AAPL\n",
      "pnl: 3.41\n",
      "2016-12-02 00:00:00\n",
      "AAPL\n",
      "pnl: 4.39\n",
      "2016-12-05 00:00:00\n",
      "AAPL\n",
      "pnl: 4.00\n",
      "2016-12-06 00:00:00\n",
      "AAPL\n",
      "pnl: 4.75\n",
      "2016-12-07 00:00:00\n",
      "AAPL\n",
      "pnl: 3.95\n",
      "2016-12-08 00:00:00\n",
      "AAPL\n",
      "pnl: 2.92\n",
      "2016-12-09 00:00:00\n",
      "AAPL\n",
      "pnl: 1.89\n",
      "2016-12-12 00:00:00\n",
      "AAPL\n",
      "pnl: 0.15\n",
      "2016-12-13 00:00:00\n",
      "AAPL\n",
      "pnl: 0.76\n",
      "2016-12-14 00:00:00\n",
      "AAPL\n",
      "pnl: -1.03\n",
      "2016-12-15 00:00:00\n",
      "AAPL\n",
      "pnl: -1.03\n",
      "2016-12-16 00:00:00\n",
      "AAPL\n",
      "pnl: -1.63\n",
      "2016-12-19 00:00:00\n",
      "AAPL\n",
      "pnl: -1.77\n",
      "2016-12-20 00:00:00\n",
      "AAPL\n",
      "pnl: -2.41\n",
      "2016-12-21 00:00:00\n",
      "AAPL\n",
      "pnl: -2.71\n",
      "2016-12-22 00:00:00\n",
      "AAPL\n",
      "pnl: -2.81\n",
      "2016-12-23 00:00:00\n",
      "AAPL\n",
      "pnl: -2.08\n",
      "2016-12-26 00:00:00\n",
      "AAPL\n",
      "pnl: -2.30\n",
      "2016-12-27 00:00:00\n",
      "AAPL\n",
      "pnl: -2.30\n",
      "2016-12-28 00:00:00\n",
      "AAPL\n",
      "pnl: -3.00\n",
      "2016-12-29 00:00:00\n",
      "AAPL\n",
      "pnl: -2.52\n",
      "2016-12-30 00:00:00\n",
      "AAPL\n",
      "pnl: -2.50\n",
      "2017-01-02 00:00:00\n",
      "AAPL\n",
      "pnl: -1.63\n",
      "2017-01-03 00:00:00\n",
      "AAPL\n",
      "pnl: -1.63\n",
      "2017-01-04 00:00:00\n",
      "AAPL\n",
      "pnl: -1.94\n",
      "2017-01-05 00:00:00\n",
      "AAPL\n",
      "pnl: -1.82\n",
      "2017-01-06 00:00:00\n",
      "AAPL\n",
      "pnl: -2.38\n",
      "2017-01-09 00:00:00\n",
      "AAPL\n",
      "pnl: -3.62\n",
      "2017-01-10 00:00:00\n",
      "AAPL\n",
      "pnl: -4.64\n",
      "2017-01-11 00:00:00\n",
      "AAPL\n",
      "pnl: -4.76\n",
      "2017-01-12 00:00:00\n",
      "AAPL\n",
      "pnl: -5.37\n",
      "2017-01-13 00:00:00\n",
      "AAPL\n",
      "pnl: -4.89\n",
      "2017-01-16 00:00:00\n",
      "AAPL\n",
      "pnl: -4.69\n",
      "2017-01-17 00:00:00\n",
      "AAPL\n",
      "pnl: -4.69\n",
      "2017-01-18 00:00:00\n",
      "AAPL\n",
      "pnl: -5.60\n",
      "2017-01-19 00:00:00\n",
      "AAPL\n",
      "pnl: -5.59\n",
      "2017-01-20 00:00:00\n",
      "AAPL\n",
      "pnl: -5.39\n",
      "2017-01-23 00:00:00\n",
      "AAPL\n",
      "pnl: -5.60\n",
      "2017-01-24 00:00:00\n",
      "AAPL\n",
      "pnl: -5.68\n",
      "2017-01-25 00:00:00\n",
      "AAPL\n",
      "pnl: -5.58\n",
      "2017-01-26 00:00:00\n",
      "AAPL\n",
      "pnl: -7.39\n",
      "2017-01-27 00:00:00\n",
      "AAPL\n",
      "pnl: -7.45\n",
      "2017-01-30 00:00:00\n",
      "AAPL\n",
      "pnl: -7.46\n",
      "2017-01-31 00:00:00\n",
      "AAPL\n",
      "pnl: -7.15\n",
      "2017-02-01 00:00:00\n",
      "AAPL\n",
      "pnl: -6.89\n",
      "2017-02-02 00:00:00\n",
      "AAPL\n",
      "pnl: -13.92\n",
      "2017-02-03 00:00:00\n",
      "AAPL\n",
      "pnl: -13.71\n",
      "2017-02-06 00:00:00\n",
      "AAPL\n",
      "pnl: -14.23\n",
      "2017-02-07 00:00:00\n",
      "AAPL\n",
      "pnl: -15.38\n",
      "2017-02-08 00:00:00\n",
      "AAPL\n",
      "pnl: -16.56\n",
      "2017-02-09 00:00:00\n",
      "AAPL\n",
      "pnl: -17.05\n",
      "2017-02-10 00:00:00\n",
      "AAPL\n",
      "pnl: -17.95\n",
      "2017-02-13 00:00:00\n",
      "AAPL\n",
      "pnl: -17.67\n",
      "2017-02-14 00:00:00\n",
      "AAPL\n",
      "pnl: -18.78\n",
      "2017-02-15 00:00:00\n",
      "AAPL\n",
      "pnl: -20.44\n",
      "2017-02-16 00:00:00\n",
      "AAPL\n",
      "pnl: -20.90\n",
      "2017-02-17 00:00:00\n",
      "AAPL\n",
      "pnl: -20.75\n",
      "2017-02-20 00:00:00\n",
      "AAPL\n",
      "pnl: -21.10\n",
      "2017-02-21 00:00:00\n",
      "AAPL\n",
      "pnl: -21.10\n",
      "2017-02-22 00:00:00\n",
      "AAPL\n",
      "pnl: -22.04\n",
      "2017-02-23 00:00:00\n",
      "AAPL\n",
      "pnl: -22.43\n",
      "2017-02-24 00:00:00\n",
      "AAPL\n",
      "pnl: -21.88\n",
      "2017-02-27 00:00:00\n",
      "AAPL\n",
      "pnl: -22.00\n",
      "2017-02-28 00:00:00\n",
      "AAPL\n",
      "pnl: -22.26\n",
      "2017-03-01 00:00:00\n",
      "AAPL\n",
      "pnl: -22.32\n",
      "2017-03-02 00:00:00\n",
      "AAPL\n",
      "pnl: -24.99\n",
      "2017-03-03 00:00:00\n",
      "AAPL\n",
      "pnl: -24.20\n",
      "2017-03-06 00:00:00\n",
      "AAPL\n",
      "pnl: -24.98\n",
      "2017-03-07 00:00:00\n",
      "AAPL\n",
      "pnl: -24.56\n",
      "2017-03-08 00:00:00\n",
      "AAPL\n",
      "pnl: -24.73\n",
      "2017-03-09 00:00:00\n",
      "AAPL\n",
      "pnl: -24.23\n",
      "2017-03-10 00:00:00\n",
      "AAPL\n",
      "pnl: -23.93\n",
      "2017-03-13 00:00:00\n",
      "AAPL\n",
      "pnl: -24.37\n",
      "2017-03-14 00:00:00\n",
      "AAPL\n",
      "pnl: -24.43\n",
      "2017-03-15 00:00:00\n",
      "AAPL\n",
      "pnl: -24.23\n",
      "2017-03-16 00:00:00\n",
      "AAPL\n",
      "pnl: -25.63\n",
      "2017-03-17 00:00:00\n",
      "AAPL\n",
      "pnl: -25.85\n",
      "2017-03-20 00:00:00\n",
      "AAPL\n",
      "pnl: -25.18\n",
      "2017-03-21 00:00:00\n",
      "AAPL\n",
      "pnl: -26.58\n",
      "2017-03-22 00:00:00\n",
      "AAPL\n",
      "pnl: -25.04\n",
      "2017-03-23 00:00:00\n",
      "AAPL\n",
      "pnl: -26.54\n",
      "2017-03-24 00:00:00\n",
      "AAPL\n",
      "pnl: -26.07\n",
      "2017-03-27 00:00:00\n",
      "AAPL\n",
      "pnl: -25.80\n",
      "2017-03-28 00:00:00\n",
      "AAPL\n",
      "pnl: -26.03\n",
      "2017-03-29 00:00:00\n",
      "AAPL\n",
      "pnl: -28.82\n",
      "2017-03-30 00:00:00\n",
      "AAPL\n",
      "pnl: -29.12\n",
      "2017-03-31 00:00:00\n",
      "AAPL\n",
      "pnl: -28.94\n",
      "2017-04-03 00:00:00\n",
      "AAPL\n",
      "pnl: -28.68\n",
      "2017-04-04 00:00:00\n",
      "AAPL\n",
      "pnl: -28.72\n",
      "2017-04-05 00:00:00\n",
      "AAPL\n",
      "pnl: -29.74\n",
      "2017-04-06 00:00:00\n",
      "AAPL\n",
      "pnl: -29.03\n",
      "2017-04-07 00:00:00\n",
      "AAPL\n",
      "pnl: -28.68\n",
      "2017-04-10 00:00:00\n",
      "AAPL\n",
      "pnl: -28.38\n",
      "2017-04-11 00:00:00\n",
      "AAPL\n",
      "pnl: -28.22\n",
      "2017-04-12 00:00:00\n",
      "AAPL\n",
      "pnl: -26.75\n",
      "2017-04-13 00:00:00\n",
      "AAPL\n",
      "pnl: -26.91\n",
      "2017-04-14 00:00:00\n",
      "AAPL\n",
      "pnl: -26.19\n",
      "2017-04-17 00:00:00\n",
      "AAPL\n",
      "pnl: -26.19\n",
      "2017-04-18 00:00:00\n",
      "AAPL\n",
      "pnl: -26.94\n",
      "2017-04-19 00:00:00\n",
      "AAPL\n",
      "pnl: -26.33\n",
      "2017-04-20 00:00:00\n",
      "AAPL\n",
      "pnl: -25.84\n",
      "2017-04-21 00:00:00\n",
      "AAPL\n",
      "pnl: -27.52\n",
      "2017-04-24 00:00:00\n",
      "AAPL\n",
      "pnl: -27.36\n",
      "2017-04-25 00:00:00\n",
      "AAPL\n",
      "pnl: -28.66\n",
      "2017-04-26 00:00:00\n",
      "AAPL\n",
      "pnl: -29.51\n",
      "2017-04-27 00:00:00\n",
      "AAPL\n",
      "pnl: -28.70\n",
      "2017-04-28 00:00:00\n",
      "AAPL\n",
      "pnl: -28.81\n",
      "2017-05-01 00:00:00\n",
      "AAPL\n",
      "pnl: -28.67\n",
      "2017-05-02 00:00:00\n",
      "AAPL\n",
      "pnl: -31.47\n",
      "2017-05-03 00:00:00\n",
      "AAPL\n",
      "pnl: -32.36\n",
      "2017-05-04 00:00:00\n",
      "AAPL\n",
      "pnl: -31.93\n",
      "2017-05-05 00:00:00\n",
      "AAPL\n",
      "pnl: -31.42\n",
      "2017-05-08 00:00:00\n",
      "AAPL\n",
      "pnl: -33.74\n",
      "2017-05-09 00:00:00\n",
      "AAPL\n",
      "pnl: -37.61\n",
      "2017-05-10 00:00:00\n",
      "AAPL\n",
      "pnl: -38.54\n",
      "2017-05-11 00:00:00\n",
      "AAPL\n",
      "pnl: -37.85\n",
      "2017-05-12 00:00:00\n",
      "AAPL\n",
      "pnl: -39.11\n",
      "2017-05-15 00:00:00\n",
      "AAPL\n",
      "pnl: -41.17\n",
      "2017-05-16 00:00:00\n",
      "AAPL\n",
      "pnl: -40.79\n",
      "2017-05-17 00:00:00\n",
      "AAPL\n",
      "pnl: -40.57\n",
      "2017-05-18 00:00:00\n",
      "AAPL\n",
      "pnl: -35.57\n",
      "2017-05-19 00:00:00\n",
      "AAPL\n",
      "pnl: -37.76\n",
      "2017-05-22 00:00:00\n",
      "AAPL\n",
      "pnl: -38.26\n",
      "2017-05-23 00:00:00\n",
      "AAPL\n",
      "pnl: -39.15\n",
      "2017-05-24 00:00:00\n",
      "AAPL\n",
      "pnl: -38.97\n",
      "2017-05-25 00:00:00\n",
      "AAPL\n",
      "pnl: -38.53\n",
      "2017-05-26 00:00:00\n",
      "AAPL\n",
      "pnl: -39.04\n",
      "2017-05-29 00:00:00\n",
      "AAPL\n",
      "pnl: -38.79\n",
      "2017-05-30 00:00:00\n",
      "AAPL\n",
      "pnl: -38.79\n",
      "2017-05-31 00:00:00\n",
      "AAPL\n",
      "pnl: -38.84\n",
      "2017-06-01 00:00:00\n",
      "AAPL\n",
      "pnl: -37.97\n",
      "2017-06-02 00:00:00\n",
      "AAPL\n",
      "pnl: -38.37\n",
      "2017-06-05 00:00:00\n",
      "AAPL\n",
      "pnl: -40.55\n",
      "2017-06-06 00:00:00\n",
      "AAPL\n",
      "pnl: -39.09\n",
      "2017-06-07 00:00:00\n",
      "AAPL\n",
      "pnl: -39.59\n",
      "2017-06-08 00:00:00\n",
      "AAPL\n",
      "pnl: -40.47\n",
      "2017-06-09 00:00:00\n",
      "AAPL\n",
      "pnl: -40.11\n",
      "2017-06-12 00:00:00\n",
      "AAPL\n",
      "pnl: -34.35\n",
      "2017-06-13 00:00:00\n",
      "AAPL\n",
      "pnl: -30.94\n",
      "2017-06-14 00:00:00\n",
      "AAPL\n",
      "pnl: -32.06\n",
      "2017-06-15 00:00:00\n",
      "AAPL\n",
      "pnl: -30.69\n",
      "2017-06-16 00:00:00\n",
      "AAPL\n",
      "pnl: -29.85\n",
      "2017-06-19 00:00:00\n",
      "AAPL\n",
      "pnl: -27.92\n",
      "2017-06-20 00:00:00\n",
      "AAPL\n",
      "pnl: -31.82\n",
      "2017-06-21 00:00:00\n",
      "AAPL\n",
      "pnl: -30.54\n",
      "2017-06-22 00:00:00\n",
      "AAPL\n",
      "pnl: -31.37\n",
      "2017-06-23 00:00:00\n",
      "AAPL\n",
      "pnl: -31.14\n",
      "2017-06-26 00:00:00\n",
      "AAPL\n",
      "pnl: -31.76\n",
      "2017-06-27 00:00:00\n",
      "AAPL\n",
      "pnl: -31.32\n",
      "2017-06-28 00:00:00\n",
      "AAPL\n",
      "pnl: -29.32\n",
      "2017-06-29 00:00:00\n",
      "AAPL\n",
      "pnl: -31.33\n",
      "2017-06-30 00:00:00\n",
      "AAPL\n",
      "pnl: -29.27\n",
      "2017-07-03 00:00:00\n",
      "AAPL\n",
      "pnl: -29.59\n",
      "2017-07-04 00:00:00\n",
      "AAPL\n",
      "pnl: -29.10\n",
      "2017-07-05 00:00:00\n",
      "AAPL\n",
      "pnl: -29.10\n",
      "2017-07-06 00:00:00\n",
      "AAPL\n",
      "pnl: -29.66\n",
      "2017-07-07 00:00:00\n",
      "AAPL\n",
      "pnl: -28.36\n",
      "2017-07-10 00:00:00\n",
      "AAPL\n",
      "pnl: -29.75\n",
      "2017-07-11 00:00:00\n",
      "AAPL\n",
      "pnl: -30.59\n",
      "2017-07-12 00:00:00\n",
      "AAPL\n",
      "pnl: -31.04\n",
      "2017-07-13 00:00:00\n",
      "AAPL\n",
      "pnl: -31.24\n",
      "2017-07-14 00:00:00\n",
      "AAPL\n",
      "pnl: -33.19\n",
      "2017-07-17 00:00:00\n",
      "AAPL\n",
      "pnl: -34.41\n",
      "2017-07-18 00:00:00\n",
      "AAPL\n",
      "pnl: -34.90\n",
      "2017-07-19 00:00:00\n",
      "AAPL\n",
      "pnl: -35.40\n",
      "2017-07-20 00:00:00\n",
      "AAPL\n",
      "pnl: -36.30\n",
      "2017-07-21 00:00:00\n",
      "AAPL\n",
      "pnl: -35.65\n",
      "2017-07-24 00:00:00\n",
      "AAPL\n",
      "pnl: -35.58\n",
      "2017-07-25 00:00:00\n",
      "AAPL\n",
      "pnl: -37.33\n",
      "2017-07-26 00:00:00\n",
      "AAPL\n",
      "pnl: -37.95\n",
      "2017-07-27 00:00:00\n",
      "AAPL\n",
      "pnl: -38.64\n",
      "2017-07-28 00:00:00\n",
      "AAPL\n",
      "pnl: -35.86\n",
      "2017-07-31 00:00:00\n",
      "AAPL\n",
      "pnl: -34.85\n",
      "2017-08-01 00:00:00\n",
      "AAPL\n",
      "pnl: -34.11\n",
      "2017-08-02 00:00:00\n",
      "AAPL\n",
      "pnl: -35.37\n",
      "2017-08-03 00:00:00\n",
      "AAPL\n",
      "pnl: -42.17\n",
      "2017-08-04 00:00:00\n",
      "AAPL\n",
      "pnl: -40.66\n",
      "2017-08-07 00:00:00\n",
      "AAPL\n",
      "pnl: -41.45\n",
      "2017-08-08 00:00:00\n",
      "AAPL\n",
      "pnl: -43.77\n",
      "2017-08-09 00:00:00\n",
      "AAPL\n",
      "pnl: -44.99\n",
      "2017-08-10 00:00:00\n",
      "AAPL\n",
      "pnl: -45.93\n",
      "2017-08-11 00:00:00\n",
      "AAPL\n",
      "pnl: -41.01\n",
      "2017-08-14 00:00:00\n",
      "AAPL\n",
      "pnl: -43.09\n",
      "2017-08-15 00:00:00\n",
      "AAPL\n",
      "pnl: -45.37\n",
      "2017-08-16 00:00:00\n",
      "AAPL\n",
      "pnl: -47.05\n",
      "2017-08-17 00:00:00\n",
      "AAPL\n",
      "pnl: -46.43\n",
      "2017-08-18 00:00:00\n",
      "AAPL\n",
      "pnl: -43.45\n",
      "2017-08-21 00:00:00\n",
      "AAPL\n",
      "pnl: -43.11\n",
      "2017-08-22 00:00:00\n",
      "AAPL\n",
      "pnl: -42.83\n",
      "2017-08-23 00:00:00\n",
      "AAPL\n",
      "pnl: -45.30\n",
      "2017-08-24 00:00:00\n",
      "AAPL\n",
      "pnl: -45.49\n",
      "2017-08-25 00:00:00\n",
      "AAPL\n",
      "pnl: -44.81\n",
      "2017-08-28 00:00:00\n",
      "AAPL\n",
      "pnl: -45.38\n",
      "2017-08-29 00:00:00\n",
      "AAPL\n",
      "pnl: -46.93\n",
      "2017-08-30 00:00:00\n",
      "AAPL\n",
      "pnl: -48.31\n",
      "[Timestamp('2015-01-02 00:00:00', freq='C'), datetime.datetime(2017, 8, 30, 0, 0)]\n"
     ]
    }
   ],
   "source": [
    "tf = MyTradingFunctions()\n",
    "tsParams = MyTradingParams(tf)\n",
    "tradingSystem = TradingSystem(tsParams)\n",
    "results = tradingSystem.startTrading()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "yCjIlVz-3r99"
   },
   "source": [
    "Results for each timestamp are stored as csv file inside the folder `./runLogs`, we also create logs inside `./tb_logs` so have a look at that as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "l9gAWcy13r9-"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'instrument_names': ['AAPL'],\n",
       " 'instrument_stats': [{'pnl': {'AAPL': -0.048736496000000004},\n",
       "   'score': {'AAPL': -1.8440700333498883}}],\n",
       " 'pnl': -0.048736496000000004,\n",
       " 'trading_days': 694,\n",
       " 'annual_return': -0.01797902430464371,\n",
       " 'annual_vol': 0.024353421654489144,\n",
       " 'sharpe_ratio': -0.7382545483636209,\n",
       " 'score': -1.8440700333498883,\n",
       " 'capitalUsage': 148.922734349998,\n",
       " 'total_profit': 340.53843999999987,\n",
       " 'maxDrawdown': 72.22124399999996,\n",
       " 'maxPortfolioValue': 1023.484748,\n",
       " 'total_loss': 389.2749359999995,\n",
       " 'variance': 2.3535283582592883,\n",
       " 'capital': 850.675049900002,\n",
       " 'count_profit': 311,\n",
       " 'portfolio_value': 951.263504,\n",
       " 'count_loss': 338}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "momentum_backtest_losing_money.ipynb",
   "provenance": []
  },
  "environment": {
   "name": "common-cpu.m46",
   "type": "gcloud",
   "uri": "gcr.io/deeplearning-platform-release/base-cpu:m46"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
